我正在开发一个键盘应用程序,但出现了问题。我想通过编程的方式在Android EditText中限制/阻止软键盘输入一些特殊字符。所以,请问有没有办法实现这个功能?
试试这个,可能对你有用
public class MainActivity extends Activity {
private EditText editText;
private String blockCharacterSet = "~#^|$%&*!";
private InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText.setFilters(new InputFilter[] { filter });
}
}
如果您想添加空格,可以在最后一位数字后面添加一个空格。
android:digits="0123456789qwertzuiopasdfghjklyxcvbnm "
这应该可以正常工作:
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
或者如果您更喜欢简单的方法:
<EditText android:inputType="text" android:digits="0123456789*,qwertzuiopasdfghjklyxcvbnm" />
请查看这个链接,它展示了如何限制 Android EditText 字段中的特殊字符。
尝试使用此代码 android:digits="abcde.....012345789"
,我认为这是最简单的方法。希望这能帮到您。
虽然有点晚了,但对其他人也许有帮助。 你可以使用XML属性而不是编程方式来限制特殊字符。在单个布局中,可能会有多个EditText,其中只想在一个EditText中限制特殊字符。因此,在xml中定义将有所帮助。以下是仅允许输入字母和数字以限制特殊字符的代码
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text10"
android:singleLine="true"
android:maxLines="1"
android:maxLength="16"
android:digits="abcdefghijklmnopqrstuvwxyz0123456789"/>
如果您在添加空格时遇到问题,请使用所有字母添加一个空格。以下是一个例子。 此外,您应该知道,在这种情况下用户无法添加新行。
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:digits="0123456789,a bcdefghijklmnopqrstuvwxyz"
android:maxLines="1"
android:singleLine="true" />
edit_field.setKeyListener(DigitsKeyListener.getInstance("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
edit_field.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
yourEditText.addTextChangedListener(new TextWatcher() {
CharSequence previous;
public void afterTextChanged(Editable s) {
if(s.toString().contains("&^%$#*&(")){
s.clear();
s.append(previous.toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
previous = s;
}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
被接受的答案仍然允许用户粘贴不需要的字符。这个解决方案是有效的:
你所需要做的就是:
List<String> unwantedChars = new ArrayList<>(Arrays.asList("@", "€"));
editText.addTextChangedListener(new FilterUnwantedChars(editText, unwantedChars));
辅助类:
public class FilterUnwantedChars implements TextWatcher {
private List<String> unwantedChars;
private EditText editText;
private boolean listenToTextChange;
public FilterUnwantedChars(EditText editText, List<String> unwantedChars) {
this.unwantedChars = unwantedChars;
this.editText = editText;
this.listenToTextChange = true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!listenToTextChange) {
listenToTextChange = true;
return;
}
String result = s.toString();
final String oldString = result;
for (String unwanted : unwantedChars) {
result = result.replace(unwanted, "");
}
if (!oldString.equals(result)) {
listenToTextChange = false;
int currentPos = editText.getSelectionStart()-1;
editText.setText(result);
try {
editText.setSelection(currentPos);
} catch (Exception e) {
// nothing
}
}
}
@Override
public void afterTextChanged(Editable s) { }
public void setEditText(EditText editText) {
this.editText = editText;
}
public EditText getEditText() {
return editText;
}
public List<String> getUnwantedChars() {
return unwantedChars;
}
public void setUnwantedChars(List<String> unwantedChars) {
this.unwantedChars = unwantedChars;
}
}
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
yourEditText. ...
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});