软件编程
位置:首页>> 软件编程>> Android编程>> Android 自定义按钮点击事件和长按事件对比

Android 自定义按钮点击事件和长按事件对比

作者:lqh  发布时间:2023-06-04 00:57:14 

标签:Android,自定义,按钮

 Android 自定义按钮点击事件和长按事件对比

一个按钮同时实现点击和长按事件,有时候会有冲突,我们针对这一现象来自定义按钮来区分点击和长按事件

1.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"
 tools:context="com.example.adfaf.MainActivity"
 android:orientation="vertical"
  >

<huahua.btnlongtouch.LongTouchBtn  
   android:id="@+id/btn2"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"  
   android:text="自定义Btn" />  

<TextView  
   android:id="@+id/tv1"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:text="0"  
   />  
   <SeekBar  
     android:id="@+id/seekbar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:max="100"

/>

</LinearLayout>

2.MainActivity中


public class MainActivity extends Activity {

private TextView Tv1;  
   private LongTouchBtn Btn1;  
   private int num=0;
   private SeekBar sbar;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     sbar= (SeekBar) findViewById(R.id.seekbar);
     Tv1 = (TextView)findViewById(R.id.tv1);  
     Btn1 = (LongTouchBtn)findViewById(R.id.btn2);  
     Btn1.setOnClickListener(new View.OnClickListener() {  

@Override  
       public void onClick(View arg0) {  
         Log.i("huahua", "自定义按钮处理单击");  

}  
     });  
     Btn1.setOnLongClickListener(new View.OnLongClickListener() {  

@Override  
       public boolean onLongClick(View v) {  
         Log.i("huahua", "自定义按钮处理长按一次相应");  
         return true;  
       }  
     });  

/**  
      * 这是一个自定义的接口 专门负责处理长按逻辑  
      *  @param listener  
      *       * 。  
      * @param time  
      *      第2个参数传入1000 ,表示1秒处理一次onLongTouch()方法  
      */  
     Btn1.setOnLongTouchListener(new LongTouchListener() {  

@Override  
       public void onLongTouch() {  
         num++;  
         int seekbar_progress = sbar.getProgress();
         Log.i("huahua", "seekbar_progress="+seekbar_progress);  
         seekbar_progress++;
         sbar.setProgress(seekbar_progress);
         Tv1.setText(num+"");  
         Log.i("huahua", "正在长按");  

}  
     },1000);  
   }  
}

3.新建一个自定义的LongTouchBtn类


public class LongTouchBtn extends Button{  

/**  
  * 记录当前自定义Btn是否按下  
  */  
 private boolean clickdown = false;  

/**  
  * 下拉刷新的回调接口  
  */  
 private LongTouchListener mListener;  

/**  
  * 按钮长按时 间隔多少毫秒来处理 回调方法  
  */  
 private int mtime;  

/**  
  * 构造函数  
  * @param context  
  * @param attrs  
  */  
 public LongTouchBtn(Context context, AttributeSet attrs) {  
   super(context, attrs);  
   // TODO Auto-generated constructor stub  
 }  

/**  
  * 处理touch事件  
  */  
 @Override  
 public boolean onTouchEvent(MotionEvent event) {  
   if(event.getAction() == MotionEvent.ACTION_DOWN)  
   {  
     clickdown = true;  
     new LongTouchTask().execute();  

Log.i("huahua", "按下");  
   }  
   else if(event.getAction() == MotionEvent.ACTION_UP)  
   {  
     clickdown = false;  
     Log.i("huahua", "弹起");  
   }  
   return super.onTouchEvent(event);  
 }  

/**  
  * 使当前线程睡眠指定的毫秒数。  
  *  
  * @param time  
  *      指定当前线程睡眠多久,以毫秒为单位  
  */  
 private void sleep(int time) {  
   try {  
     Thread.sleep(time);  
   } catch (InterruptedException e) {  
     e.printStackTrace();  
   }  
 }  

/**  
  * 处理长按的任务  
  */  
 class LongTouchTask extends AsyncTask<Void, Integer, Void>{  

@Override  
   protected Void doInBackground(Void... params) {  
     while(clickdown)  
     {  
       sleep(mtime);  
       publishProgress(0);  
     }  
     return null;  
   }  

@Override  
   protected void onPostExecute(Void result) {  

}  

@Override  
   protected void onProgressUpdate(Integer... values) {  
     mListener.onLongTouch();  
   }  

}  

/**  
  * 给长按btn控件注册一个 * 。  
  *  
  * @param listener  
  *       * 的实现。  
  * @param time  
  *      多少毫秒时间间隔 来处理一次回调方法  
  */  
 public void setOnLongTouchListener(LongTouchListener listener, int time) {  
   mListener = listener;  
   mtime = time;  

}  

/**  
  * 长按监听接口,使用按钮长按的地方应该注册此 * 来获取回调。  
  */  
 public interface LongTouchListener {  

/**  
    * 处理长按的回调方法  
    */  
   void onLongTouch();  
 }  
}  

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:http://blog.csdn.net/wu371894545/article/details/53525282

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com