1

我正在尝试开发一个在网格布局中有 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;
  }
4

0 回答 0