Android studio基础练习06【PoolWater】

一、结构搭建

二、代码实现

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_incInput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onCtrlValve"
                android:text="增加进水口" />

            <Button
                android:id="@+id/btn_decInput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onCtrlValve"
                android:text="减少进水口" />

            <TextView
                android:id="@+id/tv_numInput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textColor="#0000FF"
                android:textSize="30sp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_incOutput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onCtrlValve"
                android:text="增加出水口" />

            <Button
                android:id="@+id/btn_decOutput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onCtrlValve"
                android:text="减少出水口" />

            <TextView
                android:id="@+id/tv_numOutput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textColor="#FF0000"
                android:textSize="30sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="top|center_horizontal">

        <ProgressBar
            android:id="@+id/progressBar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_margin="10dp" />
    </LinearLayout>

</LinearLayout>

MainActivity

package com.suke.poolwater;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private int water_count = 0;
    private TextView tv_numInput, tv_numOutput;
    private ProgressBar bar;
    private ArrayList<ValveThread> InputThreads, OutputThreads;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_numInput = findViewById(R.id.tv_numInput);
        tv_numOutput = findViewById(R.id.tv_numOutput);
        bar = findViewById(R.id.progressBar);
        InputThreads = new ArrayList<>();
        OutputThreads = new ArrayList<>();
    }

    private Handler hdl_main = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == ValveThread.WATER_INPUT) {
                water_count++;

            } else if (msg.what == ValveThread.WATER_OUTPUT) {
                water_count--;
            }
            bar.setProgress(water_count);
            super.handleMessage(msg);
        }
    };

    public void onCtrlValve(View view) {
        ValveThread t = null;
        switch (view.getId()) {
            case R.id.btn_incInput:
                t = new ValveThread(InputThreads.size(), 200, ValveThread.WATER_INPUT, hdl_main);
                InputThreads.add(t);
                t.start();
                break;
            case R.id.btn_decInput:
                t = InputThreads.remove(InputThreads.size() - 1);
                t.close();
                break;
            case R.id.btn_incOutput:
                t = new ValveThread(OutputThreads.size(), 200, ValveThread.WATER_OUTPUT, hdl_main);
                OutputThreads.add(t);
                t.start();
                break;
            case R.id.btn_decOutput:
                t = OutputThreads.remove(OutputThreads.size() - 1);
                t.close();
                break;
        }
        //更新界面上阀门的数量
        tv_numInput.setText(InputThreads.size() + "");
        tv_numOutput.setText(OutputThreads.size() + "");
    }
}

ValveThread

package com.suke.poolwater;

import android.os.Handler;
import android.os.Message;

public class ValveThread extends Thread {
    public static final int WATER_INPUT = 1;
    public static final int WATER_OUTPUT = 2;
    private int id;
    private long ms;
    private int ctrl_what;
    private boolean closed;
    private Handler hdl;

    public ValveThread(int id, long ms, int ctrl_what, Handler handler) {
        this.id = id;
        this.ms = ms;
        this.ctrl_what = ctrl_what;
        this.hdl = handler;
        this.closed = false;
    }

    //关闭阀门
    public void close() {
        this.closed = true;
    }

    @Override
    public void run() {
        while (!closed) {
            //todo 向消息队列发送进水或出水的消息
            Message msg = Message.obtain();
            msg.what = ctrl_what;
            msg.arg1 = id;
            //todo 向主线程发送msg
            hdl.sendMessage(msg);
            try {
                sleep(ms);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        super.run();
    }
}

三、执行效果

猜你喜欢

转载自blog.csdn.net/qq_45037155/article/details/124925383