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");
            }
        };

相关推荐

  1. Qt篇——在线中更新ui

    2024-01-30 18:24:04       25 阅读
  2. Qt如何保证调用时候的线安全

    2024-01-30 18:24:04       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-30 18:24:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-30 18:24:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-30 18:24:04       87 阅读
  4. Python语言-面向对象

    2024-01-30 18:24:04       96 阅读

热门阅读

  1. 51单片机温湿度数据管理系统

    2024-01-30 18:24:04       60 阅读
  2. Proteus仿真软件在单片机教学中的应用

    2024-01-30 18:24:04       53 阅读
  3. gstreamer学习笔记

    2024-01-30 18:24:04       63 阅读
  4. 1.25

    1.25

    2024-01-30 18:24:04      45 阅读
  5. 抽象工厂模式深度理解,以及举例说明

    2024-01-30 18:24:04       46 阅读
  6. 网络安全攻防红队常用命令

    2024-01-30 18:24:04       42 阅读
  7. 华纳云:SQL Server中offset使用报错怎么解决

    2024-01-30 18:24:04       50 阅读
  8. treeview

    treeview

    2024-01-30 18:24:04      41 阅读
  9. redis 高可用

    2024-01-30 18:24:04       52 阅读
  10. Stream流

    Stream流

    2024-01-30 18:24:04      50 阅读