在接下来的框焦点转到在第一个框中输入后性格(focus goto on next box after

2019-09-18 03:46发布

我要实现我所设置的小应用程序edittext输入PIN号码和手机号码。 每个编辑文本包含每次1个字符。
现在,当我运行这个程序,我发现,每盒我需要给重点每个edittext
因此,在这种情况下,在第一,然后转到字符之后第三个在第二个输入的输入的字符后,可以将焦点设置在明年的EditText。
所以请帮助我这个

这里是布局图像。

Answer 1:

1. implements TextWatcher on EditTexts

然后当你EDITTEXT的文本长度为1。

2. Change focus on other EditText using

调用requestFocus()在其他的EditText ..

CODE:(此代码是你的理解,它可能是在你的情况和你不同的需要)

public class LoginActivity extends Activity implements TextWatcher
{
 .
 .
 .

  editTextUName = (EditText) findViewById(R.id.edttextusername);
  editTextUName.addTextChangedListener(this);
  editTextPwd = (EditText) findViewById(R.id.edttextpassword);
  editTextPwd.addTextChangedListener(this);

@Override
public void afterTextChanged(Editable s)
{
    if (s.toString().length() == 1)
    {
              editTextPwd.requestFocus();
    }
}


Answer 2:

使用此代码

final EditText EditText01 = (EditText) findViewById(R.id.editText01);
final EditText EditText02= (EditText) findViewById(R.id.editText02);
final EditText EditText03 = (EditText) findViewById(R.id.editText03);
final EditText EditText04 = (EditText) findViewById(R.id.editText04);

EditText01.requestFocus();
EditText01.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                    @Override
                    public void onFocusChange(final View v,
                            final boolean hasFocus) {

                        if (hasFocus) {
                            final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            // only will trigger it if no physical keyboard is
                            // open
                            mgr.showSoftInput(EditText01,
                                    InputMethodManager.SHOW_IMPLICIT);
                        }

                    }
                });

        EditText01.setOnKeyListener(new View.OnKeyListener() {

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

                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DEL) {
                        EditText01.requestFocus();
                    } else {
                        EditText02.requestFocus();
                    }
                }
                return false;
            }
        });

        EditText02.setOnKeyListener(new View.OnKeyListener() {

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

                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DEL) {
                        EditText01.requestFocus();
                    } else {
                        EditText03.requestFocus();
                    }
                }
                return false;
            }
        });
        EditText03.setOnKeyListener(new View.OnKeyListener() {

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

                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DEL) {
                        EditText02.requestFocus();
                    } else {
                        EditText04.requestFocus();
                    }

                }
                return false;
            }
        });

        EditText04.setOnKeyListener(new View.OnKeyListener() {

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

                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DEL) {
                        EditText03.requestFocus();
                    } 

                }

                return false;

            }
        });


Answer 3:

添加一个文本变化监听器

      textView1.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {

               textView2.requestFocus();

            }

            public void beforeTextChanged(CharSequence s, int start, int count,             int after) {
             }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });


Answer 4:

这应该很好地工作。 mPinFirst等等都代表每个PIN输入的EditTexts。 还应采取退格的照顾。

    mPinFirst.setOnKeyListener(onKeyListener);
    mPinSecond.setOnKeyListener(onKeyListener);
    mPinThird.setOnKeyListener(onKeyListener);
    mPinFourth.setOnKeyListener(onKeyListener);




View.OnKeyListener onKeyListener = new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {

        if(KeyEvent.KEYCODE_DEL != keyEvent.getKeyCode()) {
            if (((ESEditText) view).getText().length() == 1)
                if (view.focusSearch(View.FOCUS_RIGHT) != null) {
                    view.focusSearch(View.FOCUS_RIGHT).requestFocus();
                    return true;
                }
        }else{
            ((ESEditText) view).setText("");
            if (view.focusSearch(View.FOCUS_LEFT) != null) {
                view.focusSearch(View.FOCUS_LEFT).requestFocus();
                return true;
            }
        }
        return false;
    }
};


文章来源: focus goto on next box after enter character in first box