SeekBar 本身有就是一個View 對象,既然是View 對象那也證明它有setOnLongClickListener 事件,眾所周知,這個事件是讓一個View 對象擁有長按功能,進而達到我們想要實現的操作。
為SeekBar 添加長按事件監聽如下:
SeekBar.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
那麼,我們來試試,它是否可以成立 ,為它添加一個Toast :
Toast.makeText(ttActivty.this, "fda", 100).show();
一般來說,我們運行項目,然後長按SeekBar 即能夠把Toast Show 出來。那麼試一下吧,試了之後結果很讓人費解,因為我們知道SeekBar 繼承自AbsSeekBar 而AbsSeekBar 又是繼承自ProgressBar ,而ProgressBar 的長按事件是可行的,可是這裡我們同樣的操作卻得不到效果,Toast 並不能像我們如期想像的一樣顯示出來。那是不是就是說SeekBar 不支持長按事件呢?這點我也不好下定論,總之我們為其他同樣的View 對象的操作是可行的。好了,既然SeekBar 可能不支持長按事件,那我們就為它模擬一個長按事件,模擬思路如下:
- 1、繼承SeekBar 重寫內部的事件和方法
- 2、實現一個線程,當在規定的時間內停住即認為其是一個長按動作
- 3、實現長按事件的接口函數
- 4、並為重寫的SeekBar 添加自己的OnSeekBarChangeListener 監聽事件
具體操作看如下:
1、繼承SeekBar 重寫內部的事件和方法
代碼如下:
public class seekBarDemo extends SeekBar implements OnTouchListener
2、實現一個線程,當在規定的時間內停住即認為其是一個長按動作
這裡實現一個自己的Runable 對象,向Handler 對象發送消息,代碼如下:
/**
* 為runable 賦值
*/
runable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
do {
i++;
try {
Thread.sleep(400);
Message msg = hand.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (isStop);
}
};
之後實現一個handler 對象用來設置該重寫View 具有的長按事件
3、實現長按事件的接口函數
/**
* 獲取一個handler 對象
* @param 0代表onTouch 1代表onChange
* @param 視圖對象
* @param 進度
* @return 返回一個handler對象
*/
public Handler getHandler(final int j, final View v, final int progress) {
Handler h = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (j) {
case 0:
if (msg.arg1 == 3) {
if (longClick != null) {
longClick.onLongClick(v);
}
}
break;
case 1:
if (msg.arg1 == 1) {
pp = progress;
}
if (msg.arg1 == 2) {
if (pp != progress) {
i = 0;
}
}
if (msg.arg1 == 3) {
i = 0;
if (pp == progress) {
if (longClick != null) {
longClick.onLongClick(seekBarDemo.this);
}
}
}
break;
}
super.handleMessage(msg);
}
};
return h;
}
4、並為重寫的SeekBar 添加自己的OnSeekBarChangeListener 監聽事件
這裡我們首先定義一個onChange 接口 ,接口中有三未實現的方法,分別代表SeekBar 的三種狀態
/**
* 進度改變接口
* @author terry
*
*/
public interface onChange {
public void onStopTrackingTouch(seekBarDemo seekBar);
public void onStartTrackingTouch(seekBarDemo seekBar);
public void onProgressChanged(seekBarDemo seekBar, int progress,
boolean fromUser);
}
實現接口函數:
this.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStopTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStartTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onProgressChanged(final SeekBar seekBar,
final int progress, boolean fromUser) {
if (SeekBarChange != null) {
SeekBarChange.onProgressChanged(seekBarDemo.this, progress,
fromUser);
}
hand = getHandler(1, seekBarDemo.this, progress);
}
});
完整的參考代碼如下:
package org.lytsing.android.qzoneloading;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.SeekBar;
public class seekBarDemo extends SeekBar implements OnTouchListener {
private onLong longClick;
/**
* 長按接口
* @author terry
*
*/
public interface onLong {
public boolean onLongClick(View v);
}
private onChange SeekBarChange;
/**
* 進度改變接口
* @author terry
*
*/
public interface onChange {
public void onStopTrackingTouch(seekBarDemo seekBar);
public void onStartTrackingTouch(seekBarDemo seekBar);
public void onProgressChanged(seekBarDemo seekBar, int progress,
boolean fromUser);
}
private Handler hand;
private Runnable runable;
private Thread th;
public static int i = 0;
private boolean isStop = false;
public static int pp = 0;
public int index = 0;
public seekBarDemo(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public seekBarDemo(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnTouchListener(this);
this.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStopTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (SeekBarChange != null) {
SeekBarChange.onStartTrackingTouch(seekBarDemo.this);
}
}
@Override
public void onProgressChanged(final SeekBar seekBar,
final int progress, boolean fromUser) {
if (SeekBarChange != null) {
SeekBarChange.onProgressChanged(seekBarDemo.this, progress,
fromUser);
}
hand = getHandler(1, seekBarDemo.this, progress);
}
});
/**
* 為runable 賦值
*/
runable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
do {
i++;
try {
Thread.sleep(400);
Message msg = hand.obtainMessage();
msg.arg1 = i;
msg.sendToTarget();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (isStop);
}
};
}
/**
* 獲取一個handler 對象
* @param 0代表onTouch 1代表onChange
* @param 視圖對象
* @param 進度
* @return 返回一個handler對象
*/
public Handler getHandler(final int j, final View v, final int progress) {
Handler h = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (j) {
case 0:
if (msg.arg1 == 3) {
if (longClick != null) {
longClick.onLongClick(v);
}
}
break;
case 1:
if (msg.arg1 == 1) {
pp = progress;
}
if (msg.arg1 == 2) {
if (pp != progress) {
i = 0;
}
}
if (msg.arg1 == 3) {
i = 0;
if (pp == progress) {
if (longClick != null) {
longClick.onLongClick(seekBarDemo.this);
}
}
}
break;
}
super.handleMessage(msg);
}
};
return h;
}
/**
* 設置長按事件
* @param longClick
*/
public void setOnLongSeekBarClick(onLong longClick) {
this.longClick = longClick;
}
/**
* 設置進度改變事件
* @param change
*/
public void setOnSeekBarChange(onChange change) {
this.SeekBarChange = change;
}
@Override
public boolean onTouch(final View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isStop = true;
th = new Thread(runable);
th.start();
i = 0;
hand = getHandler(0, v, 0);
break;
case MotionEvent.ACTION_UP:
isStop = false;
break;
}
return false;
}
}
在布局的XML可以如下定義:
<org.lytsing.android.qzoneloading.seekBarDemo
android:max="200" android:id="@+id/seek" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</org.lytsing.android.qzoneloading.seekBarDemo>
功能至此完成,在前台可以像我們之前使用SeekBar 一樣使用,功能一樣,大家也可以根據自己的需求再另行擴展。這裡給出的只是一個思路。
轉自:http://www.cnblogs.com/TerryBlog/archive/2010/08/31/1813481.html