欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android studio的Handler简单实例代码

程序员文章站 2022-03-09 08:42:24
实现:edittext输入消息,通过按钮选择发送给主线程或者子线程;以下有效果图、mainactivity.java代码和activity_main.xml代码效果图:mainactivity.jav...

实现:edittext输入消息,通过按钮选择发送给主线程或者子线程;

以下有效果图、mainactivity.java代码和activity_main.xml代码

效果图:

android studio的Handler简单实例代码

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实例内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!