android studio的Handler简单实例代码

实现:EditText输入消息,通过按钮选择发送给主线程或者子线程;

以下有效果图、MainActivity.java代码和activity_main.xml代码

效果图:

android studio的Handler简单实例代码_第1张图片

MainActivity.java代码

package huan.san.handleroneapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    private TextView mTextView1;
    private TextView mTextView2;
    private Button mButton1;
    private Button mButton2;
    private Handler1 mSubThreadHandler;
    private Handler2 mMainThreadHandler;
    private EditText mEditText;
    String sMessage;
    private int counter = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init() {
        mTextView1 = (TextView)findViewById(R.id.textView1);
        mTextView2 = (TextView)findViewById(R.id.textView2);
        mButton1 = (Button) findViewById(R.id.button1);
        mButton2 = (Button) findViewById(R.id.button2);
        mEditText = (EditText)findViewById(R.id.editTextTextPersonName) ;

        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sMessage=mEditText.getText().toString();
                //主线程发送消息到子线程
                mSubThreadHandler = new Handler1(getMainLooper());
                Message message = new Message();
                message.obj = sMessage;
                mSubThreadHandler.sendMessage(message);
            }
        });
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sMessage=mEditText.getText().toString();
                //子线程发送消息到主线程
                mMainThreadHandler = new Handler2(getMainLooper());
                Message message = new Message();
                message.obj = sMessage;
                mMainThreadHandler.sendMessage(message);
            }
        });

        new Thread(){
            public void run(){
                Looper.prepare();
                //Looper.myLooper()获取当前线程的looper
                mSubThreadHandler = new Handler1(Looper.myLooper());
                Message message = new Message();
                message.obj = sMessage;
            };
        }.start();
    }
    
    public class Handler1 extends Handler{
        private Handler1(Looper looper){
            super(looper);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //子线程消息显示
            mTextView2.setText("子线程收到:" + msg.obj);
        }
    }

    public class Handler2 extends Handler{
        private Handler2(Looper looper){
            super(looper);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //主线程消息显示
            mTextView1.setText("主线程收到:" + msg.obj);

        }
    }
}

activity_main.xml代码





    

到此这篇关于android studio的Handler简单实例的文章就介绍到这了,更多相关android studio的Handler实例内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(android studio的Handler简单实例代码)