Android 在布局上查找指针位置和视图id
我正在尝试开发一个应用程序,它在网格布局中有8个按钮。需要的是,当我一次按两个按钮时,我需要获取相对于父视图的指针位置以及单击视图的id。一旦我离开任何按钮区域,也需要一个触发器 我尝试使用扩展布局并覆盖onInterceptTouchEvent。然后我可以得到两个手指的指针位置。但无法确定单击了哪个按钮,并且在移动手指时无法确定我是否超出了该按钮区域 我是第一次这样触摸工作。因此,任何帮助都将不胜感激。请帮我摆脱困境。。4天以来,我一直在努力Android 在布局上查找指针位置和视图id,android,android-canvas,android-custom-view,Android,Android Canvas,Android Custom View,我正在尝试开发一个应用程序,它在网格布局中有8个按钮。需要的是,当我一次按两个按钮时,我需要获取相对于父视图的指针位置以及单击视图的id。一旦我离开任何按钮区域,也需要一个触发器 我尝试使用扩展布局并覆盖onInterceptTouchEvent。然后我可以得到两个手指的指针位置。但无法确定单击了哪个按钮,并且在移动手指时无法确定我是否超出了该按钮区域 我是第一次这样触摸工作。因此,任何帮助都将不胜感激。请帮我摆脱困境。。4天以来,我一直在努力 public class KeypadEventH
public class KeypadEventHandler extends LinearLayout {
private Context mContext;
private TextView text1,edit;
int x1,y1,x2,y2;
private GridView mGView;
ImageView IV;
private static final String TAG=KeypadEventHandler.class.getSimpleName();
public KeypadEventHandler(Context context) {
super(context);
this.mContext=context;
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.activity_main, this);
text1=(TextView)findViewById(R.id.txtInput);
//b = (Button)findViewById(R.id.button1);
edit=(TextView)findViewById(R.id.txtMemory);
mGView=(GridView)findViewById(R.id.grdButtons);
mGView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d(TAG,"on the click event");
// TODO Auto-generated method stub
IV=(ImageView)arg1;
}
});
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int pid1=0;
int action = event.getAction();
pid1 = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
// Log.d(TAG,"on the touch event");
//this is giving the x and y locations for the both fingers. but not able to find out which botton the finger touched.
//as the click listener is not invoking after implementing onInterceptTouchEvent
x1 = (int) event.getX(0);
y1 = (int) event.getY(0);
text1.setText("test ACTION DOWN " + " values = first finger X0: "+ x1 + " first finger Y0: " + y1);
x2 = (int) event.getX(1);
y2 = (int) event.getY(1);
edit.setText("test ACTION DOWN " + " values = second finger X0: "+ x2 + " first finger Y0: " + y2);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
公共类KeypadEventHandler扩展了LinearLayout{
私有上下文;
私有文本查看文本1,编辑;
int-x1,y1,x2,y2;
私有网格视图;
ImageView IV;
私有静态最终字符串标记=KeypadEventHandler.class.getSimpleName();
公钥PadEventHandler(上下文){
超级(上下文);
this.mContext=上下文;
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
充气(R.layout.ACTIVE_main,本);
text1=(TextView)findViewById(R.id.txtInput);
//b=(按钮)findViewById(R.id.button1);
编辑=(TextView)findViewById(R.id.txtMemory);
mGView=(GridView)findviewbyd(R.id.grdButtons);
mGView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
Log.d(标记“点击事件”);
//TODO自动生成的方法存根
IV=(图像视图)arg1;
}
});
}
@凌驾
公共布尔值onInterceptTouchEvent(MotionEvent ev){
返回true;
}
@凌驾
公共布尔onTouchEvent(运动事件){
int-pid1=0;
int action=event.getAction();
pid1=动作>>动作事件.action\u指针\u ID\u移位;
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
打破
case MotionEvent.ACTION\u移动:
//Log.d(标签“触摸事件”);
//这给出了两个手指的x和y位置,但无法找出手指碰到的是哪一个手指。
//因为click侦听器在实现onInterceptTouchEvent后未调用
x1=(int)event.getX(0);
y1=(int)event.getY(0);
text1.setText(“测试动作向下”+“值=第一指X0:“+x1+”第一指Y0:+y1”);
x2=(int)event.getX(1);
y2=(int)event.getY(1);
edit.setText(“测试动作向下”+“值=第二个手指X0:“+x2+”第一个手指Y0:“+y2”);
打破
case MotionEvent.ACTION\u UP:
打破
}
返回true;
}
您好!您有解决方案吗?