【发布时间】:2015-05-29 01:57:26
【问题描述】:
当用户在列表视图上保持 2 秒时,我需要启动添加事件。之后振动设备并显示对话框询问“添加到收藏夹?”。
我正在尝试这个。
lv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.AXIS_PRESSURE){
long eventDuration =
android.os.SystemClock.elapsedRealtime()
- event.getDownTime();
//Put up the Yes/No message box
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert
.setTitle("Service")
.setMessage("Add to favorite?")
//.setIcon(R.drawable.chile1)
.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Activity.class);
startActivity(intent);
}
})
.setNegativeButton("No", null) //Do nothing on no
.show();
}
return false;
}
上面的代码显示 5 次对话框。
【问题讨论】:
-
考虑使用线程?
-
长按,还是按两下?从你的问题看不清楚。长时间点击,这个答案可能会有所帮助:stackoverflow.com/questions/8846707/…