Android Studio非UI线程修改控件——定时器软件

目录

一、UI界面设计

1、UI样式

2、XML代码

二、功能编写

1、定义

2、实现方法

3、功能实现


一、UI界面设计

1、UI样式

2、XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="#59b1ef"
    tools:context=".MainActivity4">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_client_send"
        android:text="发送"
        android:textSize="25sp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:onClick="btn_client_send_clicked" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:layout_height="300dp"
        android:layout_below="@id/btn_client_send"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#ffffff" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_start"
        android:text="开始"
        android:layout_below="@+id/text_view"
        android:textSize="25sp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:onClick="btn_start_clicked" />

    <TextView
        android:id="@+id/text_view2"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:text="0"
        android:layout_height="100dp"
        android:layout_below="@id/btn_start"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#ffffff" />


</RelativeLayout>

二、功能编写

1、定义

public TextView textView1,textView2;

public Handler h;
public Handler h2;

2、实现方法

public void btn_start_clicked(View v){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0;i < 101;i++){
                    Message msg = new Message();
                    msg.what = i;
                    h.sendMessage(msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
    }

3、功能实现

textView1 = findViewById(R.id.text_view);
        textView1.setText("数据接收框");
        textView2 = findViewById(R.id.text_view2);
        textView2.setText("0");

        h = new Handler(){//UI主线程的家里的电话,处理一些其他进程无法处理的事件。
            @Override
            public void handleMessage(Message msg) {//区分事件的类型
                super.handleMessage(msg);
                textView2.setText(msg.what+"s");
            }
        };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琪琪猫不会嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值