0
passCode1 = (EditText) findViewById(R.id.passCode1);

    passCode1.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
             if(keyCode == KeyEvent.KEYCODE_DEL){  
                 passCode1.requestFocus();
                 }
            return false;
        }
    });

我在 oncreate 方法中有上面的代码我保留了断点并尝试调试我看到 setOnKeyListner 被调用或初始化了两次。

4

3 回答 3

1
        passCode1.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
             if(keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_UP){  
                 //passCode1.setText(null);
                 passCode1.requestFocus();
                 return true;
                 }
            return false;
        }
    });

添加关键事件解决了这个问题

我的理解是它被初始化了两次,因为它必须为 BOX KEY DOWN 和 KEY UP 动作创建

那是YI添加了KEY DOWN CHECK,它仍然被初始化了两次但只使用了一次

于 2013-09-07T04:37:43.253 回答
0

try to return if you get keyListener event inside if loop as below:

  @Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                System.out.println("Return True.");
                return true;
            } else
            return false;
        }
于 2013-09-04T10:22:46.920 回答
0

试试这个方法

passCode1 = (EditText) findViewById(R.id.passCode1);

    passCode1.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
             if(keyCode == KeyEvent.KEYCODE_DEL){  
                 passCode1.requestFocus();
                  return true; // UPDATE HERE
                 }
            return false;
        }
    });
于 2013-09-04T10:16:31.943 回答