当前位置:

Android studio基础练习06PoolWater

访客 2024-04-24 1399 0

一、结构搭建

二、代码实现

activity_main.xml

  • <LinearLayoutxmlns: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

  • packagecom.suke.poolwater;
  • importandroidx.annotation.NonNull;
  • importandroidx.appcompat.app.AppCompatActivity;
  • importandroid.os.Bundle;
  • importandroid.os.Handler;
  • importandroid.os.Message;
  • importandroid.view.View;
  • importandroid.widget.ProgressBar;
  • importandroid.widget.TextView;
  • importjava.util.ArrayList;
  • publicclassMainActivityextendsAppCompatActivity{
  • privateintwater_count=0;
  • privateTextViewtv_numInput,tv_numOutput;
  • privateProgressBarbar;
  • privateArrayList<ValveThread>InputThreads,OutputThreads;
  • @Override
  • protectedvoidonCreate(BundlesavedInstanceState){
  • 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=newArrayList<>();
  • OutputThreads=newArrayList<>();
  • }
  • privateHandlerhdl_main=newHandler(){
  • @Override
  • publicvoidhandleMessage(@NonNullMessagemsg){
  • if(msg.what==ValveThread.WATER_INPUT){
  • water_count;
  • }elseif(msg.what==ValveThread.WATER_OUTPUT){
  • water_count--;
  • }
  • bar.setProgress(water_count);
  • super.handleMessage(msg);
  • }
  • };
  • publicvoidonCtrlValve(Viewview){
  • ValveThreadt=null;
  • switch(view.getId()){
  • caseR.id.btn_incInput:
  • t=newValveThread(InputThreads.size(),200,ValveThread.WATER_INPUT,hdl_main);
  • InputThreads.add(t);
  • t.start();
  • break;
  • caseR.id.btn_decInput:
  • t=InputThreads.remove(InputThreads.size()-1);
  • t.close();
  • break;
  • caseR.id.btn_incOutput:
  • t=newValveThread(OutputThreads.size(),200,ValveThread.WATER_OUTPUT,hdl_main);
  • OutputThreads.add(t);
  • t.start();
  • break;
  • caseR.id.btn_decOutput:
  • t=OutputThreads.remove(OutputThreads.size()-1);
  • t.close();
  • break;
  • }
  • //更新界面上阀门的数量
  • tv_numInput.setText(InputThreads.size()"");
  • tv_numOutput.setText(OutputThreads.size()"");
  • }
  • }
  • ValveThread

  • packagecom.suke.poolwater;
  • importandroid.os.Handler;
  • importandroid.os.Message;
  • publicclassValveThreadextendsThread{
  • publicstaticfinalintWATER_INPUT=1;
  • publicstaticfinalintWATER_OUTPUT=2;
  • privateintid;
  • privatelongms;
  • privateintctrl_what;
  • privatebooleanclosed;
  • privateHandlerhdl;
  • publicValveThread(intid,longms,intctrl_what,Handlerhandler){
  • this.id=id;
  • this.ms=ms;
  • this.ctrl_what=ctrl_what;
  • this.hdl=handler;
  • this.closed=false;
  • }
  • //关闭阀门
  • publicvoidclose(){
  • this.closed=true;
  • }
  • @Override
  • publicvoidrun(){
  • while(!closed){
  • //todo向消息队列发送进水或出水的消息
  • Messagemsg=Message.obtain();
  • msg.what=ctrl_what;
  • msg.arg1=id;
  • //todo向主线程发送msg
  • hdl.sendMessage(msg);
  • try{
  • sleep(ms);
  • }catch(InterruptedExceptione){
  • e.printStackTrace();
  • }
  • }
  • super.run();
  • }
  • }
  • 三、执行效果

    发表评论

    • 评论列表
    还没有人评论,快来抢沙发吧~