如何在android 5.1 app上同时使用触摸屏和键盘(外接键盘)?

How to use touch screen and keyboard (external keyboard) at the same on android 5.1 app?

在我的 android 应用程序中,当我添加触摸屏方法并且我想使用 onkey 方法同时读取键盘时,我刚刚丢失了其中一个。

我的问题是:如何同时使用它们。

这是我的主要 class :

public class SingleTouchActivity extends AppCompatActivity {


 EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//  setContentView(R.layout.activity_single_touch);

    setContentView(new SingleTouchEventView(this, null));
    edit= (EditText) findViewById(R.id.editText);
   }
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_0){
     edit.setText("here we go");

    }
    return super.onKeyDown(keyCode,event);

}

}

我有一个 class 触摸屏,我用绘制方法测试了它:

public class SingleTouchEventView extends View {

private Paint paint = new Paint();
private Path path = new Path();

public SingleTouchEventView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setFocusable(true);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);       
 }

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
 }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            // nothing to do
            break;
        default:
            return false;
    }
    // Schedules a repaint.
    invalidate();
    return true;
}
}

您的设备支持多点触控吗?

如果是,试试这个:(我希望这有用)

public class SingleTouchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_single_touch);

        setContentView(new SingleTouchEventView(this, null));

        try {
            ViewGroup vx = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
            EditText et = new EditText(this);
            et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            vx.addView(et);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

我找到了一个更简单的应用程序解决方案。希望它能帮助别人。 `public class MainActivity 扩展 AppCompatActivity {

   View myView;
   public boolean tmpflag = true;


   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myView= findViewById(R.id.Rec);
}
@Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = ev.getAction();

    if (action == MotionEvent.ACTION_DOWN) {

        if (tmpflag) {

           myView.setBackgroundColor(Color.RED);
            tmpflag = false;
        } else {

            myView.setBackgroundColor(Color.BLUE);
            tmpflag = true;
        }
        return true;
    }

    return super.dispatchTouchEvent(ev);
  }

  public boolean dispatchKeyEvent(KeyEvent event) {

    int action = event.getAction();
    int keycode= event.getKeyCode();


    if (action == KeyEvent.ACTION_DOWN) {
        if (keycode ==KeyEvent.KEYCODE_ENTER){


        if (tmpflag) {

            myView.setBackgroundColor(Color.RED);
            tmpflag = false;
        } else {

            myView.setBackgroundColor(Color.BLUE);
            tmpflag = true;
        } }
        return true;
    }

    return super.dispatchKeyEvent(event);
 }

} `