更新UI界面的几种方式

1.Handler方式

public class MainActivity extends AppCompatActivity {
    private TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1=findViewById(R.id.Textview1);
        new Thread(new Runnable() {
            @Override
            public void run() {
            Message msg=new Message();
            msg.obj="泥吼啊!";
            handler.sendMessage(msg);
            }
        }).start();
    }
    //Handler要写在主线程内
    Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String text= (String) msg.obj;
            textView1.setText(text);
        }
    };
}

2.不晓得怎么说

public class MainActivity extends AppCompatActivity {
    private TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1=findViewById(R.id.Textview1);
        handler.postDelayed(runnable,ms);
    }
    private long ms=1000;
    Handler handler =new Handler();
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            textView1.setText("猴赛雷!");
        }
    };
}

3.runOnUiThread

public class MainActivity extends AppCompatActivity {
    private TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1=findViewById(R.id.Textview1);
          runOnUiThread(
                         new Thread(new Runnable() {
                             @Override
                             public void run() {
                             textView1.setText("鲨雕看这里!");
                             }
                         })
                 );
    }
}

4.handler.post

public class MainActivity extends AppCompatActivity {
  private TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1=findViewById(R.id.Textview1);
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mTextView.setText("子线程通过post方法更新后的UI");
            }
        });
    }
}

来源

5.

public class MainActivity extends AppCompatActivity {
  private TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1=findViewById(R.id.Textview1);
        textView1.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mTextView.setText("子线程通过view.post方法更新后的UI");
            }
        });
    }
}

来源同上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值