android studio的Handler簡單實例代碼
實現(xiàn):EditText輸入消息,通過按鈕選擇發(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()獲取當前線程的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="沒有收到消息" 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實例內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Android WebView監(jiān)聽console錯誤信息
這篇文章主要介紹了Android WebView監(jiān)聽console錯誤信息,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Android使用Jetpack WindowManager開發(fā)可折疊設備(過程分享)
這篇文章主要介紹了Android使用Jetpack WindowManager開發(fā)可折疊設備,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11Android開發(fā)跳轉應用市場進行版本更新功能實現(xiàn)
這篇文章主要為大家介紹了Android實現(xiàn)跳轉到應用市場進行版本更新功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04Android Studio與SVN版本控制程序的協(xié)作使用指南
這篇文章主要介紹了Android Studio與SVN版本控制程序的協(xié)作使用指南,使用Gradle插件自動填寫SVN號并發(fā)布到指定目錄的方法,需要的朋友可以參考下2016-03-03Android ViewPager實現(xiàn)無限循環(huán)輪播廣告位Banner效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)無限循環(huán)輪播廣告位Banner效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android自定義view實現(xiàn)有header和footer作為layout使用的滾動控件
這篇文章主要介紹了Android自定義view實現(xiàn)有header和footer的滾動控件,可以在XML中當Layout使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-11-11