亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比

 更新時(shí)間:2017年04月27日 10:03:48   投稿:lqh  
這篇文章主要介紹了 Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比的相關(guān)資料,需要的朋友可以參考下

 Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比

一個(gè)按鈕同時(shí)實(shí)現(xiàn)點(diǎn)擊和長(zhǎng)按事件,有時(shí)候會(huì)有沖突,我們針對(duì)這一現(xiàn)象來(lái)自定義按鈕來(lái)區(qū)分點(diǎn)擊和長(zhǎng)按事件

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", "自定義按鈕處理長(zhǎng)按一次相應(yīng)");  
          return true;  
        }  
      });  
        
      /**  
       * 這是一個(gè)自定義的接口 專(zhuān)門(mén)負(fù)責(zé)處理長(zhǎng)按邏輯  
       *  @param listener  
       *      監(jiān)聽(tīng)器。  
       * @param time  
       *      第2個(gè)參數(shù)傳入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", "正在長(zhǎng)按");  
            
        }  
      },1000);  
    }  
} 

3.新建一個(gè)自定義的LongTouchBtn類(lèi)

public class LongTouchBtn extends Button{  
   
  /**  
   * 記錄當(dāng)前自定義Btn是否按下  
   */  
  private boolean clickdown = false;  
    
  /**  
   * 下拉刷新的回調(diào)接口  
   */  
  private LongTouchListener mListener;  
    
  /**  
   * 按鈕長(zhǎng)按時(shí) 間隔多少毫秒來(lái)處理 回調(diào)方法  
   */  
  private int mtime;  
    
  /**  
   * 構(gòu)造函數(shù)  
   * @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);  
  }  
  
  /**  
   * 使當(dāng)前線程睡眠指定的毫秒數(shù)。  
   *  
   * @param time  
   *      指定當(dāng)前線程睡眠多久,以毫秒為單位  
   */  
  private void sleep(int time) {  
    try {  
      Thread.sleep(time);  
    } catch (InterruptedException e) {  
      e.printStackTrace();  
    }  
  }  
    
  /**  
   * 處理長(zhǎng)按的任務(wù)  
   */  
  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();  
    }  
      
  }  
    
  /**  
   * 給長(zhǎng)按btn控件注冊(cè)一個(gè)監(jiān)聽(tīng)器。  
   *  
   * @param listener  
   *      監(jiān)聽(tīng)器的實(shí)現(xiàn)。  
   * @param time  
   *      多少毫秒時(shí)間間隔 來(lái)處理一次回調(diào)方法  
   */  
  public void setOnLongTouchListener(LongTouchListener listener, int time) {  
    mListener = listener;  
    mtime = time;  
      
  }  
    
  /**  
   * 長(zhǎng)按監(jiān)聽(tīng)接口,使用按鈕長(zhǎng)按的地方應(yīng)該注冊(cè)此監(jiān)聽(tīng)器來(lái)獲取回調(diào)。  
   */  
  public interface LongTouchListener {  
  
    /**  
     * 處理長(zhǎng)按的回調(diào)方法  
     */  
    void onLongTouch();  
  }  
}  

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論