【发布时间】:2018-01-25 19:55:23
【问题描述】:
我想要的是EditText 只接受一个输入值,即数字并具有密码属性。我还想,如果我输入值,比如在第一个EditText,我应该关注下一个EditText。同样,如果我按下软键盘的删除按钮,它的焦点应该向后退,比如从EditText2 到EditText 1。混合所有这些,密码属性不能正常工作。
并且不要在没有实际尝试代码的情况下投反对票。
这是我现在的布局。
<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 代码::
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);
【问题讨论】:
-
您面临的问题是什么?
-
无法将输入值屏蔽为密码属性所做的“*”。
-
在activity中写ontextchange方法之前是否有效
-
@SibinDavis 是的,addTextChangedListener 搞砸了。
-
那么请接受答案
标签: android android-edittext addtextchangedlistener