我目前正在试验 onclick 监听器和 on touch 监听器。
我发现即使用户将他/她的手指从元素上拖开,使用 onclick 应用程序也将始终执行该操作。但我希望用户能够改变主意并将他/她的手指从元素上拖开,这样它就不会执行操作。我设法做到了这一点。
触摸代码:
action_bar_group_info.setOnTouchListener(new View.OnTouchListener() {
int [] location = new int[4];
@Override
public boolean onTouch(View v, MotionEvent event) {
location[0] = action_bar_group_info.getLeft();
location[1] = action_bar_group_info.getTop();
location[2] = action_bar_group_info.getBottom();
location[3] = action_bar_group_info.getRight();
Log.v("Location: ", "top: "+String.valueOf(location[1]) + " left: "+String.valueOf(location[0]) + " right: "+String.valueOf(location[3]) + " bottom: "+String.valueOf(location[2]));
if (event.getAction() == MotionEvent.ACTION_DOWN) {
action_bar_group_info.setBackgroundColor(Color.parseColor("#ec9169"));
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if ((int) event.getX() > location[0] && (int) event.getX() < location[3] && (int) event.getY() > location[1] && (int) event.getY() < location[2]) {
action_bar_group_info.setBackgroundColor(Color.parseColor("#ec9169"));
Intent intent = new Intent(mContext, GroupInformation.class);
intent.putExtra("gid", gid);
intent.putExtra("gname", gname);
intent.putExtra("image", img);
intent.putExtra("uid", uid);
startActivity(intent);
finish();
}else {
action_bar_group_info.setBackgroundColor(Color.parseColor("#f54c00"));
}
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
Log.v("Location pointer: x:"+ String.valueOf((int) event.getX())," Y: "+ String.valueOf((int) event.getY()));
if ((int) event.getX() < location[0] || (int) event.getX() > location[3] || (int) event.getY() < location[1] || (int) event.getY() > location[2]) {
action_bar_group_info.setBackgroundColor(Color.parseColor("#f54c00"));
}
}
return false;
}
});
但这真的是这样做的方法吗?没有更简单的方法来实现我想要的吗?
action_bar_group_info 是线性布局。