zoukankan      html  css  js  c++  java
  • android 子线程使用handle修改主线线程内容

    1、子线程使用handle修改主线线程内容简单案例

    1)、activity_handle.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=".HandleActivity">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="129dp"
            android:layout_marginTop="69dp"
            android:text="@string/显示内容"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="116dp"
            android:layout_marginTop="248dp"
            android:text="@string/button2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    2、HandleActivity.java

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class HandleActivity extends AppCompatActivity {
        private Button btn;
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_handle);
    
            btn = findViewById(R.id.button3);
            tv = findViewById(R.id.textView);
    
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new Thread(){
    
                        @Override
                        public void run() {
                                super.run();
                            Message msg=new Message();
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }
                    }.start();
                }
            });
        }
    
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==1){
                    tv.setText("子线程使用handle修改主线程控件内容");
                }
            }
        };
    }

     方法二:

        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Toast.makeText(getApplicationContext(), (String) msg.obj, Toast.LENGTH_SHORT).show();
            }
        };

    发送消息:

      Message message = Message.obtain();
      message.obj = str;
      handler.sendMessage(message);
  • 相关阅读:
    不同数据类型在不同编译器下字节大小
    gbk/utf8/gb2312 编码中汉字字母数字所占字节数
    剑指Offer-12 矩阵中的路径
    螺旋矩阵(数组)问题(网易考点)
    C++ 多继承导致的指针偏移问题
    面试题--链表实现插入排序
    C++ 二叉树的深度优先遍历(前中后序)以及广度优先遍历
    (转)软连接和硬链接作用以及区别
    TCP/IP网络五层结构理解以及数据传输流程的理解图示
    常考知识点:进程与线程,多进程与多线程
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/11407467.html
Copyright © 2011-2022 走看看