Android 更新UI的两种方法——handler和runOnUiThread() - $firecat的代码足迹$ - 博客频道 - CSDN.NET
文章来源:http://www.2cto.com/kf/201302/190591.html
Android 更新UI的两种方法——handler和runOnUiThread()
public class MainActivity extends Activity { private EditText UITxt; private Button updateUIBtn; private UIHandler UIhandler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); UITxt = (EditText)findViewById(R.id.ui_txt); updateUIBtn = (Button)findViewById(R.id.update_ui_btn); updateUIBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub UIhandler = new UIHandler(); UIThread thread = new UIThread(); thread.start(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private class UIHandler extends Handler{ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Bundle bundle = msg.getData(); String color = bundle.getString("color"); UITxt.setText(color); } } private class UIThread extends Thread{ @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("color", "黄色"); msg.setData(bundle); MainActivity.this.UIhandler.sendMessage(msg); } } }
FusionField.currentActivity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), , "Update My UI", Toast.LENGTH_LONG).show(); } });
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。