android studio的Handler简单实例代码
实现:EditText输入消息,通过按钮选择发送给主线程或者子线程;
以下有效果图、MainActivity.java代码和activity_main.xml代码
效果图:
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代码
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="60dp" android:layout_marginTop="140dp" android:text="主线程发送消息" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="220dp" android:layout_marginTop="140dp" android:text="子线程发送消息" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="137dp" android:layout_height="97dp" android:layout_marginStart="220dp" android:layout_marginTop="216dp" android:text="没有收到消息" android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView1" android:layout_width="139dp" android:layout_height="94dp" android:layout_marginStart="60dp" android:layout_marginTop="216dp" android:text="没有收到消息" android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editTextTextPersonName" android:layout_width="200dp" android:layout_height="51dp" android:layout_marginStart="104dp" android:layout_marginTop="64dp" android:ems="10" android:inputType="textPersonName" android:text="请输入" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
到此这篇关于android studio的Handler简单实例的文章就介绍到这了,更多相关android studio的Handler实例内容请搜索海外IDC网以前的文章或继续浏览下面的相关文章希望大家以后多多支持海外IDC网!
【来源:http://www.1234xp.com/hwgf.html 转载请保留连接】