在布局上查找指针位置和视图ID

时间:2012-11-05 12:34:50

标签: android android-canvas android-custom-view

我正在尝试开发一个在Grid-layout中有8个按钮的应用程序。需要的是当我一次触摸两个按钮时,我需要获得指向父视图的指针位置以及单击哪个视图的id。我离开任何按钮区域时也需要触发器。

我尝试使用扩展布局并覆盖onInterceptTouchEvent。然后onTouchEvent我能够获得两个手指的指针位置。但是无法找到点击的按钮,并且在移动手指时无法找到我的按钮区域。

我是第一次尝试这种方式。所以任何帮助都会非常感激。请帮助我解决这个问题。我4天来一直在努力。

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;
  }

0 个答案:

没有答案