利用Handler在子线程中更新UI

TestHandler.java

public class TestHandler extends Activity {
    static  final  String UPPER_NUM="upper";
    private  CalThreand calThreand;
    EditText editText;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_handler);
        editText=(EditText)findViewById(R.id.handler_parText1);
        textView=(TextView)findViewById(R.id.handler_text);

        calThreand=new CalThreand();
        calThreand.start();

        Button sendButton=(Button)findViewById(R.id.handler_sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=editText.getText().toString();
                if(str==null||str.equals(""))
                    return ;
                Message msg=new Message();
                msg.what=0x123;
                Bundle bundle=new Bundle();
                bundle.putInt(UPPER_NUM,Integer.parseInt(str));
                msg.setData(bundle);
                calThreand.mHandler.sendMessage(msg);
            }
        });

    }
    class CalThreand extends  Thread
    {
        public Handler mHandler;
        public void run()
        {
            Looper.prepare();
            mHandler=new Handler()
            {
               @Override
               public void handleMessage(Message msg){
                    if(msg.what==0x123){
                        int upper=msg.getData().getInt(UPPER_NUM);
                       final  List<Integer>nums=new ArrayList<Integer>();
                        for(int i=1;i<=upper;i++)
                            nums.add(i);
                     runOnUiThread(new Runnable() {
                         @Override
                         public void run() {
                             textView.setText(nums.toString());
                         }
                     });
                    }
                }
            };
            Looper.loop();
        }
    }

}


activity_test_handler.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal">
           <EditText
               android:inputType="number"
               android:hint="a:"
               android:id="@+id/handler_parText1"
               android:layout_width="0dp"
               android:layout_weight="1"
               android:layout_height="wrap_content"
               />
           <EditText
               android:inputType="number"
               android:hint="b:"
               android:id="@+id/handler_parText2"
               android:layout_weight="1"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
             />
       </LinearLayout>
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/handler_sendButton"
            android:text="send"
            android:background="@drawable/send_button_selector"
            android:layout_width="match_parent"
            android:textColor="#ffffff"
            android:textSize="30sp"
            android:layout_height="wrap_content"/>
</TableRow>
    <TextView
        android:id="@+id/handler_text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textSize="25sp"
        />
</LinearLayout>


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/Thereisnospon/p/4768460.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值