在子线程中操作Ui的几种小方法

 在MainActivity中的子线程不能操作UI 所以我总结几种比较简单实用的方法来实现在子线程中对Ui进行修改的方法

方法:

1.handler的使用

2.imgeView.post()或者textView.post()方法

3.MainActivity.this.runOnUiThread()方法

废话不多说,看代码:

package demo.liuchen.com.okhttp;

import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView text;
    private ImageView image;
    private String url = "http://bz.budejie.com/?typeid=2&ver=3.4.3&no_cry=1&client=android&c=search&a=hot&location=1";
    private String imgurl = "http://t2.27270.com/uploads/tu/201606/32/k5xnewfzvz0.jpg";

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
           String Ibody = (String) msg.obj;
            text.setText(Ibody);
        }
    };

    private Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
         byte [] data  = (byte[]) msg.obj;
            image.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length));
        }
    };





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

        text = (TextView) findViewById(R.id.tv);
        image = (ImageView) findViewById(R.id.imageview);

    }

    public void downLoadText(View view) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("TAG","onFailure"+e.getMessage().toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //注意 此处response只能获取一次,第二次获取会为null
               final String body =  response.body().string();
                //在子线程操作UI两种都可以
                //handler也可以
//               text.post(new Runnable() {
//                   @Override
//                   public void run() {
//                       text.setText(body);
//                   }
//               });


//                MainActivity.this.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        text.setText(body);
//                    }
//                });


               Message msg = new Message();
                msg.obj= body;
                handler.sendMessage(msg);
            }
        });
    }

    public void downLoadImage(View view) {
        OkHttpClient okHttpClient = new OkHttpClient();
         Request request = new Request.Builder()
                 .url(imgurl)
                 .build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("TAG","onFailure"+e.getMessage().toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//              byte[]  data=  new byte[1024];
//              data = response.body().bytes();
                final byte[] data = response.body().bytes();
//                image.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        image.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length));
//                    }
//                });

//                MainActivity.this.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        image.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length));
//                    }
//                });


                Message msg1 = new Message();
                msg1.obj = data;
                handler1.sendMessage(msg1);



            }
        });
    }
}

 
 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值