线程通信之handle用法

主显示布局以及代码:

activity_main.xml:

注意:这里为什么把%之多少放在主线程呢,因为像这种textView改变ui值的,不允许在工作线程操作,不然会报错

通俗点讲:所有的更新UI操作都需要在主线程(也就是UI线程中完成),而不能在新开的子线程中操作(ProgressBar除外

<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">


    <Button 
        android:id="@+id/btDownload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击下载"
        />
    <ProgressBar 
        android:id="@+id/pbDownload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="2"
        style="@android:style/Widget.ProgressBar.Horizontal"
        />
<TextView 
   android:id="@+id/tvDownload"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="0%"/>

</LinearLayout>

java代码:

package com.litsoft.main;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

private ProgressBar pbDownload;
private TextView tvDownload;
private Handler handler;

private final static int START_DOWNING = 0;
private final static int FINISH_DOWNING = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initHandler();
setListener();
}





private void setListener() {
// TODO Auto-generated method stub
findViewById(R.id.btDownload).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
new Thread(){


@Override
public void run() {
// TODO Auto-generated method stub
for (int i=0;i<=100;i++){
SystemClock.sleep(200);
Message message = Message.obtain();
message.what = START_DOWNING;
message.arg1 = i;
pbDownload.setProgress(i);
handler.sendMessage(message);
}
Message message = Message.obtain();
message.what = FINISH_DOWNING;
handler.sendMessage(message);
}

}.start();
}
});
}






private void initView() {
// TODO Auto-generated method stub
pbDownload = (ProgressBar) findViewById(R.id.pbDownload);
tvDownload = (TextView) findViewById(R.id.tvDownload);
}


private void initHandler() {
// TODO Auto-generated method stub
handler = new Handler(){


@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case START_DOWNING:
tvDownload.setText(msg.arg1+"%");
break;
case FINISH_DOWNING:
Toast.makeText(MainActivity.this, "下载完成",20000).show();
break;
}

}

};
}
}

效果:



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值