由于没有窗口焦点而导致丢弃事件:KeyEvent

时间:2016-03-21 06:00:03

标签: android android-edittext

请帮帮我。过去一天,我一直在努力解决这个问题。我不知道如何解决这个问题。虽然我尝试在edittext中键入任何字符,但键入了键,但未在必填字段上设置输入字符。当我尝试在文本更改侦听器中使用settext()设置字符时,它通过stackoverflow错误。 注意:它只发生在4.4 kitkat(提到micromax设备),但在检查nexus时工作正常)

 edtPwd1 = (EditText) findViewById(R.id.edt_pwd_1);
    edtPwd2 = (EditText) findViewById(R.id.edt_pwd_2);
    edtPwd3 = (EditText) findViewById(R.id.edt_pwd_3);
    edtPwd4 = (EditText) findViewById(R.id.edt_pwd_4);
    edtPwd1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            edtPwd1.setFocusable(true);
            edtPwd1.setCursorVisible(true);
            return false;
        }
    });
    edtPwd2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            edtPwd2.setFocusable(true);
            edtPwd2.setCursorVisible(true);
            return false;
        }
    });
    edtPwd3.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            edtPwd3.setFocusable(true);
            edtPwd3.setCursorVisible(true);
            return false;
        }
    });
    edtPwd4.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            edtPwd4.setFocusable(true);
            edtPwd4.setCursorVisible(true);
            return false;
        }
    });
    edtPwd1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.e("sdfsadfsdfsdf------", "-------" + s);
           // edtPwd1.requestFocus();
        }

        @Override
        public void afterTextChanged(Editable s) {
            //edtPwd1.setText(s);
            edtPwd1.clearFocus();
            edtPwd2.requestFocus();
        }
    });
    edtPwd2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.e("efasdhfhsldf-----", "------1" + s);
            edtPwd2.requestFocus();
        }

        @Override
        public void afterTextChanged(Editable s) {
            //edtPwd2.setText(s);
            edtPwd2.clearFocus();
            edtPwd3.requestFocus();
        }
    });
    edtPwd3.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.e("efasdhfhsldf-----", "------2" + s);
            edtPwd3.requestFocus();
        }

        @Override
        public void afterTextChanged(Editable s) {
            //edtPwd3.setText(s);
            edtPwd3.clearFocus();
            edtPwd4.requestFocus();
        }
    });
    edtPwd4.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.e("efasdhfhsldf-----", "------4" + s);
            edtPwd4.requestFocus();
        }

        @Override
        public void afterTextChanged(Editable s) {
            //edtPwd4.setText(s);
            if (!edtPwd4.getText().toString().isEmpty()) {
                edtPwd4.clearFocus();
                edtPwd1.clearFocus();
                edtPwd2.clearFocus();
                edtPwd3.clearFocus();
                edtPwd1.setFocusable(false);
                edtPwd2.setFocusable(false);
                edtPwd3.setFocusable(false);
                edtPwd4.setFocusable(false);
                edtPwd1.setFocusableInTouchMode(true);
                edtPwd2.setFocusableInTouchMode(true);
                edtPwd3.setFocusableInTouchMode(true);
                edtPwd4.setFocusableInTouchMode(true);
                edtPwd1.setCursorVisible(false);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(edtPwd4.getWindowToken(), 0);
            }
        }
    });
    edtPwd2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                if (edtPwd1.getText().toString().equals("")) {
                    edtPwd2.clearFocus();
                    edtPwd1.requestFocus();
                }
            }
        }
    });
    edtPwd3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                if (edtPwd1.getText().toString().equals("")) {
                    edtPwd3.clearFocus();
                    edtPwd1.requestFocus();

                } else if (edtPwd2.getText().toString().equals("")) {
                    edtPwd3.clearFocus();
                    edtPwd2.requestFocus();
                }
            }
        }
    });
    edtPwd4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                if (edtPwd1.getText().toString().equals("")) {
                    edtPwd4.clearFocus();
                    edtPwd1.requestFocus();

                } else if (edtPwd2.getText().toString().equals("")) {
                    edtPwd4.clearFocus();
                    edtPwd2.requestFocus();

                } else if (edtPwd3.getText().toString().equals("")) {
                    edtPwd4.clearFocus();
                    edtPwd3.requestFocus();
                }
            }
        }
    });

抱歉我的英语很差,并提前致谢

1 个答案:

答案 0 :(得分:0)

TextChangeListner将填充输入文本的复杂性,这取决于所有方法都被覆盖。此外,默认情况下,所有Android版本都会在用户按下时处理对EditText的关注。除非开发人员未通过代码修改默认功能,否则虚拟键盘也将可用。

首先,为了验证您是否能够在EditText中输入文本,请仅保留以下内容并对所有其他实现进行评论。

edtPwd1 = (EditText) findViewById(R.id.edt_pwd_1);
edtPwd2 = (EditText) findViewById(R.id.edt_pwd_2);
edtPwd3 = (EditText) findViewById(R.id.edt_pwd_3);
edtPwd4 = (EditText) findViewById(R.id.edt_pwd_4);

现在,如果您想要动态处理一个编辑文本输入的文本,可以尝试以下示例。这将通过处理程序

将从一个编辑文本输入的文本复制到另一个
EditText text1;EditText text2;
 StringBuffer previousChar=new StringBuffer();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        text1=(EditText)findViewById(R.id.editText);
        text2=(EditText)findViewById(R.id.editText2);
        text1.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                    @Override
                    public void afterTextChanged(Editable s) {
                        if(!previousChar.toString().equals(s.toString())){
                            Message msg=new Message();
                            msg.obj=s.toString();
                            localHandler.sendMessage(msg);
                            previousChar=new StringBuffer(s.toString());
                            Log.i("TAG", "TextEntered = "+s);
                        }
                    }
                });}

声明处理程序以获取输入的字符串

private Handler localHandler = new Handler(){
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String value=(String)msg.obj;
            text2.setText(value);
        }
    };