Android 实现按钮在没有触摸事件时几秒钟之后自动隐藏

时间:2022-11-28 17:23:15

实现按钮在没有触摸事件时几秒钟之后自动隐藏

 大概意思就是用一个线程来控制时间。

 下面我实现一个Button的自动隐藏

 

 获取控件

        mButton = (Button)findViewById(R.id.btn);

 

控件的隐藏需要有一个Handler机制监听消息和发送消息

 

   class MyHandlerextends Handler{

    //发送消息的id

    privatefinal int MSG_HIDE = 0x001;

    //WeakReference垃圾回收机制

    privateWeakReference<MainActivity>weakRef;

    public MyHandler(MainActivitypMainActivity){

        weakRef =newWeakReference<MainActivity>(pMainActivity);

    }

   

    @Override

    publicvoid handleMessage(Message msg) {

        final MainActivity mainActivity =weakRef.get();

 

        if (mainActivity !=null) {

                switch (msg.what) {

                caseMSG_HIDE:

                   mainActivity.hide();

                   break;

 

                }

            }

        super.handleMessage(msg);

    }

   

    /**

     * 发送消息

     */

    publicvoid sendHideControllMessage(){

        obtainMessage(MSG_HIDE).sendToTarget();

    }

   

然后需要开启一个新的线程,控制按钮的定时隐藏

 

 

 privateclass CountTimeThreadextends Thread{

    privatefinal long maxVisibleTime ;

    privatelong startVisibleTime;

   

    /**

     * 设置控件显示时间second单位是秒

     * @param second

     */

    public CountTimeThread(int second){

        maxVisibleTime = second * 1000;//换算为毫秒

       

        setDaemon(true);//设置为后台进程

    }

   

    /**

     * 如果用户有操作,则重新开始计时隐藏时间

     */

    privatesynchronized void reset() {

             startVisibleTime = System.currentTimeMillis();

        }

   

    @Override

    publicvoid run() {

        startVisibleTime = System.currentTimeMillis();//初始化开始时间

       

             while (true) {

             //如果时间达到最大时间,则发送隐藏消息

                if (startVisibleTime +maxVisibleTime < System.currentTimeMillis()){

                   mHandler.sendHideControllMessage();

                  

                   startVisibleTime = System.currentTimeMillis();

                }

               

                try {

                   Thread.sleep(1000);

                } catch (Exception e) {

                    

                }

            }

            

      }

}

 

 

然后需要一个开启线程的方法

privatevoid startCountTimeThread(){

    countTimeThread =new CountTimeThread(2);

    countTimeThread.start();

    }

 

 

重写触摸监听,如果有触摸事件发送,则需要重置显示的时间

@Override

    publicboolean onTouchEvent(MotionEvent event) {

   

    countTimeThread.reset();//重置时间

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            boolean isVisible = (mButton.getVisibility() == View.VISIBLE);

            if (!isVisible) {

                mButton.setVisibility(View.VISIBLE);

                returntrue;

            }

        }

    returnsuper.onTouchEvent(event);

   }

 

 

 

 

 

 

 

 

 

 

 

全部代码如下:

import java.lang.ref.WeakReference;

 

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Button;

 

public classMainActivity extends ActionBarActivity {

 

    ButtonmButton;

    MyHandlermHandler= newMyHandler(MainActivity.this);

    CountTimeThreadcountTimeThread;

   

   @Override

   protectedvoidonCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mButton = (Button) findViewById(R.id.btn);

        startCountTimeThread();

   }

 

   //如果有触摸事件,,则重置显示时间

   @Override

   publicbooleanonTouchEvent(MotionEvent event) {

   

    countTimeThread.reset();//重置时间

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            boolean isVisible = (mButton.getVisibility() ==View.VISIBLE);

            if (!isVisible) {

                mButton.setVisibility(View.VISIBLE);

                returntrue;

            }

        }

    return super.onTouchEvent(event);

   }

   

   /**

     * 开启监听控件隐藏的线程

     */

   privatevoidstartCountTimeThread(){

    countTimeThread = new CountTimeThread(2);

    countTimeThread.start();

   }

   

   /**

     * 隐藏需要隐藏的按钮

     */

   privatevoidhide(){

    if (mButton.getVisibility() == View.VISIBLE) {

            mButton.setVisibility(View.GONE);

        }

   }

 

   /**

     * Handler消息传递机制

     */

   classMyHandlerextendsHandler{

    //发送消息的id

    private final int MSG_HIDE= 0x001;

    //WeakReference垃圾回收机制

    private WeakReference<MainActivity>weakRef;

    public MyHandler(MainActivity pMainActivity){

       weakRef = newWeakReference<MainActivity>(pMainActivity);

    }

   

    @Override

    public void handleMessage(Message msg) {

       final MainActivity mainActivity =weakRef.get();

 

       if (mainActivity !=null) {

                switch (msg.what) {

                caseMSG_HIDE:

                   mainActivity.hide();

                   break;

 

                }

            }

       super.handleMessage(msg);

    }

   

    /**

     * 发送消息

     */

    public void sendHideControllMessage(){

       obtainMessage(MSG_HIDE).sendToTarget();

    }

   

   }

    

  

   /**

     * 用进程监听按钮控件的显示时间,定时隐藏

     * @authorzxl

     *

     */

   privateclassCountTimeThreadextends Thread{

    private final long maxVisibleTime;

    private long startVisibleTime;

   

    /**

     * 设置控件显示时间 second单位是秒

     * @param second

     */

    public CountTimeThread(int second){

       maxVisibleTime = second * 1000;//换算为毫秒

      

       setDaemon(true);//设置为后台进程

    }

   

    /**

     * 如果用户有操作,则重新开始计时隐藏时间

     */

    private synchronized void reset() {

             startVisibleTime = System.currentTimeMillis();

        }

   

    @Override

    public void run() {

       startVisibleTime = System.currentTimeMillis();//初始化开始时间

      

             while (true) {

             //如果时间达到最大时间,则发送隐藏消息

                if (startVisibleTime +maxVisibleTime < System.currentTimeMillis()){

                   mHandler.sendHideControllMessage();

                  

                   startVisibleTime = System.currentTimeMillis();

                }

               

                try {

                   Thread.sleep(1000);

                }catch(Exception e) {

                    

                }

            }

            

    }

   }

   

   

   

   

   

}