onKeyListener和TextWatcher都不能接收软键盘输入

时间:2015-06-24 03:21:07

标签: java android android-edittext textwatcher

我看过很多帖子,并且已经问过类似的问题。但是解决方案都没有奏效。首先我尝试使用TextWatcher,但在许多帖子中,它表示它不适用于软键盘。所以我尝试使用public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{ private MainThread thread; private EditText editText; private Bitmap textBitmap; public GamePanel(Context context){ super(context); //Add callback to the surfaceview to intercept events getHolder().addCallback(this); //Make GamePanel focusable so it can handle events setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format,int width, int height){} @Override public void surfaceDestroyed(SurfaceHolder holder){} @Override public void surfaceCreated(SurfaceHolder holder){ editText = new EditText(getContext()); editText.setSingleLine(true); editText.setImeOptions(EditorInfo.IME_ACTION_DONE); editText.setDrawingCacheEnabled(true); editText.layout(0, 0, WIDTH - 200, 100); editText.buildDrawingCache(); textBitmap = Bitmap.createBitmap(editText.getDrawingCache()); thread = new MainThread(getHolder(), this); //Start the game loop thread.setRunning(true); thread.start(); /*editText.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { Toast.makeText(getContext(), "ABCD", Toast.LENGTH_SHORT).show(); System.out.println("KEY PRESSED"); } return true; } });*/ /*editText.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { System.out.println("KEY PRESSED"); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { System.out.println("KEY PRESSED"); } });*/ editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { System.out.println("ABC"); return true; } return false; } }); } @Override public boolean onTouchEvent(MotionEvent event){ if(event.getAction() == MotionEvent.ACTION_DOWN ){ editText.setFocusableInTouchMode(true); editText.requestFocus(); InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); //imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); return true; } return super.onTouchEvent(event); } @Override public void draw(Canvas canvas){ if (canvas != null) { canvas.drawBitmap(textBitmap,50,50,null); canvas.restoreToCount(savedState); } } ,但它仍然没有打印出来。

npm info YOUR_PACKAGE version

使用canvas.drawBitmap绘制EditText是否与那些无效的解决方案有关?或者在实施它们时是否有任何错误?

欢迎任何解决方案,如果可能需要解释。谢谢!

编辑:尝试使用onEditorActionListener

1 个答案:

答案 0 :(得分:0)

首先检查一下 您是否在EditText内的xml中包含此行

android:singleLine="true"

如果没有,请先添加。 例如,我想在我的软键盘中使用Done按钮,这是你可以做的。

<EditText 
    android:id="edName"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:singleLine="true"    //this line to take input in single line (if you don't include this line, Done button will not take any actions)
    android:imeOptions="actionDone"/>   //this line to provide done button

现在从键盘输入完成按钮的输入,在findViewById

中的onCreate之后添加此代码
//TODO softKey "Done" listener for keyboard
edName.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
          if (actionId == EditorInfo.IME_ACTION_DONE) {
                //Do what you want here
                return true;
     }
     return false;
  }
});