使用AsyncTask 解决线程阻塞的问题

activity页面:

public class ThriedActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTv1;
    private Button mMBt_but1;
    private Button runtoright;
    private int first = 0;
    private int second = 1500;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thired);
        initView();


        mMBt_but1.setOnClickListener(this);
        TranslateAnimation animation = new TranslateAnimation(first, second, 0, 0);
        animation.setRepeatCount(20);
        animation.setDuration(2000);

        runtoright.setAnimation(animation);



    }

    private void initView() {
        mTv1 = (TextView) findViewById(R.id.tv1);
        mMBt_but1 = (Button) findViewById(R.id.btn1);
        runtoright = (Button) findViewById(R.id.runtoright);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //让主线线程睡
//                        try {
//                            Thread.sleep(5000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        int sum = 10;
//                        mMBt_but1.setText("显示:" + sum);
                          new ChangeAsyncTask().execute();
                    }
                }).start();

                break;
        }
    }

    private class ChangeAsyncTask extends AsyncTask{
        @Override
        protected Object doInBackground(Object[] objects) {
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int sum=10;
            return sum;

        }

        @Override
        protected void onPostExecute(Object o) {
            mMBt_but1.setText("显示:" + o.toString());
            super.onPostExecute(o);
        }
    }
}

 

 

 

xml页面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
        <Button android:id="@+id/runtoright"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="向右移动"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn9"/>

</LinearLayout>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值