EditText只有数字并隐藏输入值

时间:2017-08-17 11:54:59

标签: android android-edittext addtextchangedlistener

我想要的是EditText只接受一个输入值,这是一个数字并具有密码属性。我还想,如果我输入值,请先说EditText,我应该关注下一个EditText。类似地,如果我按下软键盘的删除按钮,它的焦点应该反过来说,从EditText 2到EditText 1.混合所有这些并且密码属性不能正常工作。 如果没有实际尝试代码,请不要downvote。

这是我现在的布局。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_10"
        android:orientation="horizontal"
        android:weightSum="1">

        <EditText
            android:id="@+id/pinc_1"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.16"
            android:background="@drawable/edittext_border"
            android:gravity="center"
            android:inputType="numberPassword"
            android:maxLength="1"
            android:textColor="@color/border_color" />

        <EditText
            android:id="@+id/pinc_2"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.16"
            android:background="@drawable/edittext_border"
            android:gravity="center"
            android:inputType="numberPassword"
            android:maxLength="1"
            android:textColor="@color/border_color" />

        <EditText
            android:id="@+id/pinc_3"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.16"
            android:background="@drawable/edittext_border"
            android:gravity="center"
            android:inputType="numberPassword"
            android:maxLength="1"
            android:textColor="@color/border_color" />

        <EditText
            android:id="@+id/pinc_4"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.16"
            android:background="@drawable/edittext_border"
            android:gravity="center"
            android:inputType="numberPassword"
            android:maxLength="1"
            android:textColor="@color/border_color" />

        <EditText
            android:id="@+id/pinc_5"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.16"
            android:background="@drawable/edittext_border"
            android:gravity="center"
            android:inputType="numberPassword"
            android:maxLength="1"
            android:textColor="@color/border_color" />

        <EditText
            android:id="@+id/pinc_6"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="0.16"
            android:background="@drawable/edittext_border"
            android:gravity="center"
            android:inputType="numberPassword"
            android:maxLength="1"
            android:textColor="@color/border_color" />

    </LinearLayout>

Java Code ::

pinc_1.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (pinc_1.length() == 1) {
                pinc_1.clearFocus();
                pinc_2.requestFocus();
                pinc_2.setCursorVisible(true);
            } else {

            }
        }

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

        public void afterTextChanged(Editable s) {

        }
    });

    pinc_2.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (pinc_2.length() == 1) {

                pinc_2.clearFocus();
                pinc_3.requestFocus();
                pinc_3.setCursorVisible(true);
            } else {

                pinc_2.clearFocus();
                pinc_1.requestFocus();
                pinc_1.setCursorVisible(true);
            }
        }

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

        }

        public void afterTextChanged(Editable s) {

        }
    });

    pinc_3.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (pinc_3.length() == 1) {

                pinc_3.clearFocus();
                pinc_4.requestFocus();
                pinc_4.setCursorVisible(true);
            } else {

                pinc_3.clearFocus();
                pinc_2.requestFocus();
                pinc_2.setCursorVisible(true);
            }
        }

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

        }

        public void afterTextChanged(Editable s) {

        }
    });

    pinc_4.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (pinc_4.length() == 1) {

                pinc_4.clearFocus();
                pinc_5.requestFocus();
                pinc_5.setCursorVisible(true);
            } else {

                pinc_4.clearFocus();
                pinc_3.requestFocus();
                pinc_3.setCursorVisible(true);
            }
        }

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

        }

        public void afterTextChanged(Editable s) {

        }
    });

    pinc_5.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (pinc_5.length() == 1) {

                pinc_5.clearFocus();
                pinc_6.requestFocus();
                pinc_6.setCursorVisible(true);
            } else {

                pinc_5.clearFocus();
                pinc_4.requestFocus();
                pinc_4.setCursorVisible(true);
            }
        }

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


        }

        public void afterTextChanged(Editable s) {

        }
    });

    pinc_6.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (pinc_6.length() == 1) {

                pinc_6.clearFocus();
                InputMethodManager inputManager =
                        (InputMethodManager) getActivity().
                                getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(
                        getActivity().getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            } else {

                pinc_6.clearFocus();
                pinc_5.requestFocus();
                pinc_5.setCursorVisible(true);
            }
        }

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

        }

        public void afterTextChanged(Editable s) {

        }
    });

我也尝试过这种方式::

    pinc_2.setFilters(new InputFilter[]{back_filter_pin2, new InputFilter.LengthFilter(1)});
InputFilter back_filter_pin2 = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            if (end == 0) {
                pinc_1.requestFocus();
            } else if (end == 1) {
                pinc_2.clearFocus();
                pinc_3.requestFocus();

            }

            return source;
        }
    };

也尝试过:

  pinc_6.setInputType(InputType.TYPE_CLASS_NUMBER |
  InputType.TYPE_NUMBER_VARIATION_PASSWORD);

3 个答案:

答案 0 :(得分:2)

首先使用你的条件

public void afterTextChanged(Editable s) {

}

要在删除文本时将光标聚焦到上一个字段,您可以像这样检查长度为0 ......

 if (pinc_2.length() == 1) {
    pinc_2.clearFocus();
    pinc_3.requestFocus();
    pinc_3.setCursorVisible(true);
 } else if (pinc_2.length() == 0) {
    pinc_2.clearFocus();
    pinc_1.requestFocus();
    pinc_1.setCursorVisible(true);
 } 

我测试了它......它会起作用...享受编码:)

修改

所有EditText的完整代码

我小赌注为第6个EditText添加了一些内容..

pinc_1.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        public void afterTextChanged(Editable s) {
            if (s.length() == 1) {
                pinc_1.clearFocus();
                pinc_2.requestFocus();
                pinc_2.setCursorVisible(true);
            } else {

            }
        }
    });

    pinc_2.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {

            if (s.length() == 1) {
                pinc_2.clearFocus();
                pinc_3.requestFocus();
                pinc_3.setCursorVisible(true);
            } else if (s.length() == 0) {
                pinc_2.clearFocus();
                pinc_1.requestFocus();
                pinc_1.setCursorVisible(true);
            }
        }
    });

    pinc_3.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {

            if (s.length() == 1) {
                pinc_3.clearFocus();
                pinc_4.requestFocus();
                pinc_4.setCursorVisible(true);
            } else if (s.length() == 0) {
                pinc_3.clearFocus();
                pinc_2.requestFocus();
                pinc_2.setCursorVisible(true);
            }
        }
    });

    pinc_4.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {

            if (s.length() == 1) {
                pinc_4.clearFocus();
                pinc_5.requestFocus();
                pinc_5.setCursorVisible(true);
            } else if (s.length() == 0) {
                pinc_4.clearFocus();
                pinc_3.requestFocus();
                pinc_3.setCursorVisible(true);
            }
        }
    });

    pinc_5.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {

            if (s.length() == 1) {
                pinc_5.clearFocus();
                pinc_6.requestFocus();
                pinc_6.setCursorVisible(true);
            } else if (s.length() == 0) {
                pinc_5.clearFocus();
                pinc_4.requestFocus();
                pinc_4.setCursorVisible(true);
            }
        }
    });


    pinc_6.addTextChangedListener(new TextWatcher() {

        CharSequence privText = "";

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

        }

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

        public void afterTextChanged(Editable s) {

            if (s.length() > 1) {
                pinc_6.setText(privText);
            } else if (s.length() == 0) {
                pinc_6.clearFocus();
                pinc_5.requestFocus();
                pinc_5.setCursorVisible(true);
            }
        }
    });

答案 1 :(得分:1)

首先将editText xml输入类型设为数字密码。

  

机器人:的inputType = “numericPassword”

及其大小

  

机器人:最大长度= “1”

对于editText然后..

editText1.setOnKeyListener(new OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(editText1.getText().length() == 1)
            editText2.requestFocus();
        return false;
    }
});

答案 2 :(得分:1)

将您的代码更改为

pinc_1.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
            if (pinc_1.length() == 1) {
                pinc_1.clearFocus();
                pinc_2.requestFocus();
                pinc_2.setCursorVisible(true);
            } else {

            }
        }
    });

    pinc_2.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
            if (pinc_2.length() == 1) {

                pinc_2.clearFocus();
                pinc_3.requestFocus();
                pinc_3.setCursorVisible(true);
            } else {

                pinc_2.clearFocus();
                pinc_1.requestFocus();
                pinc_1.setCursorVisible(true);
            }
        }
    });

    pinc_3.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
            if (pinc_3.length() == 1) {

                pinc_3.clearFocus();
                pinc_4.requestFocus();
                pinc_4.setCursorVisible(true);
            } else {

                pinc_3.clearFocus();
                pinc_2.requestFocus();
                pinc_2.setCursorVisible(true);
            }
        }
    });

    pinc_4.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {
 // TODO Auto-generated method stub
            if (pinc_4.length() == 1) {

                pinc_4.clearFocus();
                pinc_5.requestFocus();
                pinc_5.setCursorVisible(true);
            } else {

                pinc_4.clearFocus();
                pinc_3.requestFocus();
                pinc_3.setCursorVisible(true);
            }
        }
    });

    pinc_5.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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


        }

        public void afterTextChanged(Editable s) {
 // TODO Auto-generated method stub
            if (pinc_5.length() == 1) {

                pinc_5.clearFocus();
                pinc_6.requestFocus();
                pinc_6.setCursorVisible(true);
            } else {

                pinc_5.clearFocus();
                pinc_4.requestFocus();
                pinc_4.setCursorVisible(true);
            }
        }
    });

    pinc_6.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

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

        }

        public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
            if (pinc_6.length() == 1) {

                pinc_6.clearFocus();
                InputMethodManager inputManager =
                        (InputMethodManager) getActivity().
                                getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(
                        getActivity().getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            } else {

                pinc_6.clearFocus();
                pinc_5.requestFocus();
                pinc_5.setCursorVisible(true);
            }
        }
    });

工作正常。我测试了它。