2

我必须实现一个应用程序,其中我edittext为输入 PIN 码和手机号码设置了小。每个编辑文本一次包含 1 个字符。
现在,当我运行这个应用程序时,我发现对于每个框,我需要将焦点放在每个edittext.
因此,在这种情况下,是否可以在第一个输入字符后在下一个编辑文本中设置焦点,然后在第二个输入字符后转到第三个。
所以请帮我解决这个问题

这是布局图像。

在此处输入图像描述

4

4 回答 4

4
1. implements TextWatcher on EditTexts

然后当你得到 Edittext 的文本长度是 1

2. Change focus on other EditText using

调用requestFocus()另一个 EditText..

代码:(此代码仅供您理解,根据您的情况和需要可能会有所不同)

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();
    }
}
于 2012-06-15T12:45:32.483 回答
0

使用此代码

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;

            }
        });
于 2012-06-15T13:08:23.553 回答
0

添加文本更改侦听器

      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) {
            }
        });
于 2015-09-02T11:32:20.860 回答
0

这应该可以正常工作。mPinFirst 等是代表每个 Pin 条目的 EditText。还应该注意退格。

    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;
    }
};
于 2017-10-29T17:49:47.007 回答