标签:android io os ar java for sp on art
============问题描述============
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();
============解决方案1============
============解决方案2============
============解决方案3============
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(); } }); } }
============解决方案4============
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); } } }
标签:android io os ar java for sp on art
原文地址:http://www.cnblogs.com/lengyanyue39/p/4033849.html