android studio的Handler簡(jiǎn)單實(shí)例代碼
實(shí)現(xiàn):EditText輸入消息,通過(guò)按鈕選擇發(fā)送給主線程或者子線程;
以下有效果圖、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();
//主線程發(fā)送消息到子線程
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();
//子線程發(fā)送消息到主線程
mMainThreadHandler = new Handler2(getMainLooper());
Message message = new Message();
message.obj = sMessage;
mMainThreadHandler.sendMessage(message);
}
});
new Thread(){
public void run(){
Looper.prepare();
//Looper.myLooper()獲取當(dāng)前線程的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="主線程發(fā)送消息"
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="子線程發(fā)送消息"
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="沒(méi)有收到消息"
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="沒(méi)有收到消息"
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="請(qǐng)輸入"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
到此這篇關(guān)于android studio的Handler簡(jiǎn)單實(shí)例的文章就介紹到這了,更多相關(guān)android studio的Handler實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
這篇文章主要介紹了Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-01-01
Android RecycleView實(shí)現(xiàn)Item拖拽效果
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。本文將介紹通過(guò)RecyclerView實(shí)現(xiàn)Item拖拽效果以及拖拽位置保存,感興趣的可以參考一下2022-01-01
Android使用Flutter實(shí)現(xiàn)錄音插件
這篇文章主要介紹了基于flutter實(shí)現(xiàn)錄音功能,介紹了如何錄音,如何把文件存放到本地,這些都是我們平常使用這個(gè)功能會(huì)遇到的問(wèn)題。在使用的過(guò)程中遇到的問(wèn)題也有列出,需要的朋友可以參考下2022-08-08
解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見(jiàn)問(wèn)題
這篇文章主要介紹了解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見(jiàn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
2014值得推薦的10個(gè)移動(dòng) Web 應(yīng)用程序開(kāi)發(fā)框架
今天這篇文章向大家推薦10大優(yōu)秀的移動(dòng) Web 開(kāi)發(fā)框架,幫助開(kāi)發(fā)者更加高效的開(kāi)發(fā)移動(dòng)Web應(yīng)用。2014-08-08
android 仿微信demo——登錄功能實(shí)現(xiàn)(移動(dòng)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android帶圓形數(shù)字進(jìn)度的自定義進(jìn)度條示例
本篇文章主要介紹了Android帶圓形數(shù)字進(jìn)度的自定義進(jìn)度條示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Android開(kāi)發(fā)實(shí)現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法,結(jié)合實(shí)例形式分析了Launcher3相關(guān)配置文件與功能函數(shù)修改設(shè)置操作技巧,需要的朋友可以參考下2017-11-11
Android通過(guò)多點(diǎn)觸控的方式對(duì)圖片進(jìn)行縮放的實(shí)例代碼
這篇文章主要介紹了Android通過(guò)多點(diǎn)觸控的方式對(duì)圖片進(jìn)行縮放的實(shí)例代碼,完成了點(diǎn)擊圖片就能瀏覽大圖的功能,并且在瀏覽大圖的時(shí)候還可以通過(guò)多點(diǎn)觸控的方式對(duì)圖片進(jìn)行縮放。2018-05-05

