繁体   English   中英

onKeyListener和TextWatcher均无法接收软键盘输入

[英]Neither onKeyListener nor TextWatcher able to receive soft keyboard inputs

我看了很多帖子,并提出了类似的问题。 但是,没有一种解决方案有效。 首先,我尝试使用onKeyListener ,但是在许多帖子中,都指出它不适用于软键盘。 因此,我尝试改用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);
        }
    }

使用canvas.drawBitmap绘制EditText是否与那些解决方案不起作用有关? 还是在执行它们时有任何错误?

欢迎任何解决方案,如有可能,请解释。 谢谢!

编辑:尝试使用onEditorActionListener

首先检查您是否在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

现在要从键盘输入完成按钮,请在onCreate findViewById之后添加此代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM