如何在Android 5.1应用程序上同时使用触摸屏和键盘(外部键盘)?

时间:2016-06-24 08:13:08

标签: java android keyboard touchscreen onkeydown

在我的Android应用程序中,当我添加触摸screeen方法,我想用onkey方法读取相同的键盘,我只是失去了其中之一。

我的问题是:我怎样才能同时使用它们。

这是我的主要课程:

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

}

}

我有一个触摸屏课程,我用绘图方法测试:

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

2 个答案:

答案 0 :(得分:0)

您的设备是否支持多点触控?

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

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

    }
}

答案 1 :(得分:0)

我找到了一个更简单的应用程序解决方案。希望它会帮助某人。      `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);
 }

} `