为了账号安全,请及时绑定邮箱和手机立即绑定

使用 getCurrentFocus 时 requestFocus 不会改变焦点

使用 getCurrentFocus 时 requestFocus 不会改变焦点

万千封印 2021-11-03 10:44:54
我正在制作一个 OTPActivity并且需要实现删除功能,我有以下功能应该被onKey退格按钮触发。我的想法是我可以getCurrentFocus用来知道当前正在填充哪些字段,然后requestFocus用来选择之前的字段,但它似乎不起作用,我有另一个功能goToNext可以正常工作,所以我很困惑为什么。我正在读取字节如下:        try(FileInputStream f = new FileInputStream("nameOfFile")){            int readByte = 0;            int mask =0b111;            while((readByte=f.read())!=-1) {                System.out.println(Integer.toBinaryString(readByte & mask));            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }但是,当我尝试获取构成数字的 8 位时,有时我会得到 7 位甚至比 8 位更多的位。有人可以帮我解决这个问题吗?我想获得 8 位,以便之后我可以查看前 3 位。我需要它来返回此方法中的字符集。 public void goToPrevious() {    EditText currentFocus = (EditText) getCurrentFocus();    if (currentFocus == otpOne) {        return;    }    if (currentFocus == otpTwo) {        otpOne.requestFocus();        return;    }    if (currentFocus == otpThree) {        otpTwo.requestFocus();        return;    }    if (currentFocus == otpFour) {        otpThree.requestFocus();        return;    }}下面是goToNext工作正常的代码。public void goToNext() {    EditText currentFocus = (EditText) getCurrentFocus();    if (currentFocus == otpOne) {        otpTwo.requestFocus();        return;    }    if (currentFocus == otpTwo) {        otpThree.requestFocus();        return;    }    if (currentFocus == otpThree) {        otpFour.requestFocus();        return;    }    if (currentFocus == otpFour) {        return;    }}
查看完整描述

2 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

由于某些非常奇怪的原因,处理程序内部的值与不同,我必须通过将 的值getCurrentFocus直接传递给它来修复它,下面是我最终得到的代码。goToPreviousgetCurrentFocusonKeygoToPrevious


public void goToNext(EditText currentFocus) {


    if (currentFocus == otpOne) {

        otpTwo.requestFocus();


        return;

    }


    if (currentFocus == otpTwo) {

        otpThree.requestFocus();


        return;

    }


    if (currentFocus == otpThree) {

        otpFour.requestFocus();


        return;

    }


    if (currentFocus == otpFour) {

        return;

    }

}


public void goToPrevious(EditText currentFocus) {


    if (currentFocus == otpOne) {

        return;

    }


    if (currentFocus == otpTwo) {

        otpOne.requestFocus();


        return;

    }


    if (currentFocus == otpThree) {

        otpTwo.requestFocus();


        return;

    }


    if (currentFocus == otpFour) {

        otpThree.requestFocus();


        return;

    }

}



@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

    Log.i("ENOUGH", "onKey: " + keyCode + " " + event.getAction() + " " + KeyEvent.KEYCODE_DEL);

    if (event.getAction() == KeyEvent.ACTION_DOWN) {

        EditText currentFocus = (EditText) getCurrentFocus();

        switch (event.getKeyCode()) {

            case KeyEvent.KEYCODE_DEL: {

                if (currentFocus.getText().length() > 0) {

                    currentFocus.setText(null);

                } else {

                    goToPrevious(currentFocus);


                    EditText focused = (EditText) getCurrentFocus();

                    focused.setText(null);

                }


                return false;

            }

            case KeyEvent.KEYCODE_FORWARD:

                return true;

            case KeyEvent.KEYCODE_BACK:

                return true;

            case KeyEvent.KEYCODE_ENTER:

                return true;

            default: {

                currentFocus.setText("");

                goToNext(currentFocus);

            }

        }

    }

    return false;

}


查看完整回答
反对 回复 2021-11-03
?
侃侃尔雅

TA贡献1801条经验 获得超16个赞

您可以像下面这样使用:


et1.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(s.length()==1)

                    {

                        et2.requestFocus();

                    }

                    else if(s.length()==0)

                    {

                        et1.clearFocus();

                    }

                }

            });


            et2.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(s.length()==1)

                    {

                        et3.requestFocus();

                    }

                    else if(s.length()==0)

                    {

                        et1.requestFocus();

                    }

                }

            });


            et3.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(s.length()==1)

                    {

                        et4.requestFocus();

                    }

                    else if(s.length()==0)

                    {

                        et2.requestFocus();

                    }

                }

            });


            et4.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(s.length()==1)

                    {

                        et5.requestFocus();

                    }

                    else if(s.length()==0)

                    {

                        et3.requestFocus();

                    }

                }

            });


            et5.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(s.length()==1)

                    {

                        et6.requestFocus();

                    }

                    else if(s.length()==0)

                    {

                        et4.requestFocus();

                    }

                }

            });


            et6.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(s.length()==1)

                    {

                        et6.clearFocus();

                    }

                    else if(s.length()==0)

                    {

                        et5.requestFocus();

                    }

                }

            });



查看完整回答
反对 回复 2021-11-03
  • 2 回答
  • 0 关注
  • 227 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号