Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java onKeyListener和TextWatcher都无法接收软键盘输入_Java_Android_Android Edittext_Textwatcher - Fatal编程技术网

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

Java onKeyListener和TextWatcher都无法接收软键盘输入,java,android,android-edittext,textwatcher,Java,Android,Android Edittext,Textwatcher,我看过很多帖子,也有人问过类似的问题。但所有的解决方案都不起作用。首先,我尝试过使用onkeylister,但在许多帖子中,都说它不适用于软键盘。因此,我尝试使用TextWatcher,但它仍然无法打印任何内容 public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{ private MainThread thread; private EditText editText; private Bitm

我看过很多帖子,也有人问过类似的问题。但所有的解决方案都不起作用。首先,我尝试过使用
onkeylister
,但在许多帖子中,都说它不适用于软键盘。因此,我尝试使用
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

首先检查 您是否将这一行包含在编辑文本的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

我根本不使用xml来编程edittext。我将edittext设置为位图,并将其绘制在canvasOkay上,而不管您的edittext名称是什么,以编程方式设置此属性
edittext.setImeOptions(EditorInfo.IME\u ACTION\u DONE)并尝试按照我的建议放置
setOnEditorActionListener
。我已经在SurfaceCreated块中编辑了我的代码,如帖子所示。但“完成”按钮不会出现在软键盘上。有什么想法吗?是不是显示了其他按钮而不是…?是的,按右下角的“回车”按钮
//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;
  }
});