80,469
社区成员




private int i = 0;
private int x = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_layout);
Button b = (Button) findViewById(R.id.button);
final TextView v2 = (TextView) findViewById(R.id.t2);
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
v2.setText(x++ + "");
};
}.start();
public class MainActivity extends Activity {
private Button incButton;
private TextView msgTextView;
private int count = 0;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msgTextView = (TextView)findViewById(R.id.msgTextView);
incButton = (Button)findViewById(R.id.incButton);
incButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyTask().execute();
}
});
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
msgTextView.setText((String)msg.obj);
}
};
}
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
public Void doInBackground(Void... params) {
count++;
Message msg = handler.obtainMessage();
msg.obj = String.format("%d", count);
msg.sendToTarget();
return null;
}
}
}
private int x = 0;
private MyHandler handler =new MyHandler() ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_layout);
Button b = (Button) findViewById(R.id.button);
final TextView v2 = (TextView) findViewById(R.id.t2);
new MyThread().start();
}
private class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
if(msg.what==100){
v2.setText(x++ + "");
}
}
}
private class MyThread extends Thread{
public void run() {
while(true){
handler.sendEmptyMessage(100);
}
}
}
public class MainActivity extends Activity {
private Button incButton;
private TextView msgTextView;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msgTextView = (TextView)findViewById(R.id.msgTextView);
incButton = (Button)findViewById(R.id.incButton);
incButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyTask().execute();
}
});
}
private class MyTask extends AsyncTask<Void, Void, String> {
@Override
public String doInBackground(Void... params) {
count++;
return String.format("%d", count);
}
@Override
public void onPostExecute(String msg) {
msgTextView.setText(msg);
}
}
}
public class MainActivity extends Activity {
private Button incButton;
private TextView msgTextView;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msgTextView = (TextView)findViewById(R.id.msgTextView);
incButton = (Button)findViewById(R.id.incButton);
incButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTask<Void, Void, String> thread = new AsyncTask<Void, Void, String>() {
@Override
public String doInBackground(Void... params) {
count++;
return String.format("%d", count);
}
@Override
public void onPostExecute(String msg) {
msgTextView.setText(msg);
}
};
thread.execute();
}
});
}
}