Android之Handler 最简单的代码实现Handler

快来打我* 2022-02-14 05:40 385阅读 0赞

首先先写一个简单的布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/btn_1"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_marginTop="20dp"
  13. android:text="Button"/>
  14. <TextView
  15. android:id="@+id/textView"
  16. android:layout_width="wrap_content"
  17. android:layout_height="match_parent"
  18. android:text="Hello World!" />
  19. </LinearLayout>

之后就是编写主代码了

  1. package com.example.handlepro;
  2. import android.os.Handler;
  3. import android.os.Message;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.TextView;
  9. public class MainActivity extends AppCompatActivity {
  10. private static final String TAG = "MainActivity";
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. final TextView textView;
  14. super.onCreate (savedInstanceState);
  15. setContentView (R.layout.activity_main);
  16. textView = findViewById (R.id.textView);
  17. final Handler handler = new Handler (){ //创建Handler
  18. @Override
  19. public void handleMessage(Message msg) {
  20. super.handleMessage (msg);
  21. Log.d (TAG, "handleMessage: "+msg.what);
  22. if(msg.what == 1001){
  23. textView.setText ("Fuck");
  24. }
  25. }
  26. };
  27. findViewById (R.id.btn_1).setOnClickListener (new View.OnClickListener () {
  28. @Override
  29. public void onClick(View v) {
  30. new Thread (new Runnable () { //子线程
  31. @Override
  32. public void run() {
  33. handler.sendEmptyMessage (1001);
  34. }
  35. }).start (); //启动线程
  36. }
  37. });
  38. }
  39. }

主线程接收到子线程的消息,然后去处理,就可以实现文本的改变了

其实最简单简洁的代码是这样的

  1. package com.example.handlepro;
  2. import android.os.Handler;
  3. import android.os.Message;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.TextView;
  9. public class MainActivity extends AppCompatActivity {
  10. private static final String TAG = "MainActivity";
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. final TextView textView;
  14. super.onCreate (savedInstanceState);
  15. setContentView (R.layout.activity_main);
  16. Handler handler = new Handler (){
  17. @Override
  18. public void handleMessage(Message msg) {
  19. super.handleMessage (msg);
  20. Log.d (TAG, "handleMessage: "+msg.what);
  21. }
  22. };
  23. textView = findViewById (R.id.textView);
  24. findViewById (R.id.btn_1).setOnClickListener (new View.OnClickListener () {
  25. @Override
  26. public void onClick(View v) {
  27. new Thread (new Runnable () {
  28. @Override
  29. public void run() {
  30. textView.setText ("Fuck");
  31. }
  32. }).start ();
  33. }
  34. });
  35. }
  36. }

直接就可以运行啦

发表评论

表情:
评论列表 (有 0 条评论,385人围观)

还没有评论,来说两句吧...

相关阅读

    相关 androidhandler

    Android程序中一些操作是不能放在activity中的,因为非常耗时,如下载过程。这时,需要使用handler,即重新启动一个与activity并行的线程,下面是使用han