Android Thread modify EditText

后端 未结 5 2113
一向
一向 2021-01-14 04:34

I am having a problem with modifying EditText in another function started by the thread:

Thread thRead = new Thread( new Runnable(){
    public void run(){
          


        
5条回答
  •  别那么骄傲
    2021-01-14 04:56

    Except runOnUiThread() (which works), there is also another way, which I know of:

    Define a handler in your UI (Activity) class:

    public class MainActivity extends Activity {
    
    .....
    
        Handler uiThreadHandler = new Handler() {
            public void handleMessage(Message msg) {
                Object o = msg.obj;
                if (o==null) o = "";
                TextView textIn = (TextView)findViewById(R.id.textin);
                textIn.setText(o.toString());
            }
        };
    
    }
    

    and from inside some thread you can call it:

    Message msg = uiThreadHandler.obtainMessage();
    msg.obj = "Text for EditView";
    uiThreadHandler.sendMessage(msg);
    

提交回复
热议问题