gpt4 book ai didi

Android 对 edittext 的限制

转载 作者:行者123 更新时间:2023-11-30 03:36:46 24 4
gpt4 key购买 nike

我需要对EditText的输入设置一些限制,只允许用户输入a-z,A-Z,0-9,分号;汉字.

我的布局很简单,一个edittext和一个Button,如果edittext输入符合要求,按钮就会显示并触发事件。

我写了下面的代码,但其中有一些错误,有些功能无法实现,所以我需要有人的帮助,任何帮助将不胜感激。

public class MainActivity extends Activity {
private static String tag = "MainActivity";
private Button btn;
private EditText edt, content;
private final int[] code = { 8, 13, 32, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 186 };
private String digits = "0123456789abcdefghijklmnopqrstuvwxyz;";
private String tmp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_edit);
btn.setEnabled(false);
edt = (EditText) findViewById(R.id.edt);
edt.addTextChangedListener(edt_watcher);
// edt.setOnKeyListener(input);
btn.setOnClickListener(l);
}

//My first try:Keyboard to monitor events,Monitoring whether the keyCode is in the button collection of the permit
//The issue is once i push something not in collection,the input content being refreshed
OnKeyListener input = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
for (int i = 0; i < code.length; i++) {
if (keyCode != code) {
edt.setText(tmp);
edt.invalidate();
}
}
return false;
}
};
//my second try:Text input to monitor,after the listen i get edt.gettext then do splicing processing,after that i set back to edittext.

//issue:everytime input legal characters will add to the left side.the cursor always keep in left side,any suggestions here?
// Text input to monitor
TextWatcher edt_watcher = new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String str = s.toString();
String edttext = edt.getText().toString();
StringBuffer sb = new StringBuffer();
// TODO Auto-generated method stub
if (s.length() > 0 && str.matches("[a-zA-Z_0-9;]+")
|| str.matches("[\u4e00-\u9fa5]+")) {
sb = sb.append(s);
edt.setText(sb.toString());
Log.v(tag, sb.toString()+"======sb");
btn.setEnabled(true);
} else {
edttext = sb.append(tmp).toString();
Log.v(tag, edttext + "======edttext");
edt.setText(edttext);
tmp = str.substring(0,s.toString().length()-2);
edt.setText(tmp);
btn.setEnabled(false);
}
Log.v(tag, s + "======s");
}

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

Log.v(tag, s + "======beforeTextChanged");
}


//My third try:as comment blow,I don't know how to achieve do all the chinese letters contain in digits
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Log.v(tag, s + "======afterTextChanged");
/*String str = s.toString();
if (str.equals(tmp)) {
return;
// if `tmp==str` return,as i set the result like this,else will be endless loop.
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (digits.indexOf(str.charAt(i)) >= 0) {
// judge input characters allowed or not
sb.append(str.charAt(i));
//if allowed,add to the result ,else skip it
}
tmp = sb.toString();// set tmp,next line also can trggier it
edt.setText(tmp);// set result
edt.invalidate();
}
if ((str.matches("[a-zA-Z_0-9;]+") || str
.matches("[\u4e00-\u9fa5]+"))) {
sb.toString().substring(str.length());
}
tmp = sb.toString();
edt.setText(tmp);
edt.invalidate();*/
}
};
//judge if input is Chinese characters,the method not be used
private boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
String edttext = edt.getText().toString();
StringBuffer sb = new StringBuffer(edttext);
sb.append(c);
edt.setText(sb);
return true;
}
return false;
}
///judge if input is number or letters or ;,the method not be used
private boolean isRightData(CharSequence s) {
if (s.toString().matches("[0-9;]+")
|| s.toString().matches("[a-zA-Z]+")) {
String edttext = edt.getText().toString();
StringBuffer sb = new StringBuffer(edttext);
sb.append(s);
edt.setText(sb);
edt.invalidate();
return true;
}
return false;
}

OnClickListener l = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, IldmActivity.class);
intent.putExtra("edt", edt.getText().toString());
startActivityForResult(intent, 10);
Log.v(tag, edt.getText() + "intent");
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10 && resultCode == RESULT_OK) {
}
};
}

最佳答案

下面的代码只接受 0 到 9 位数字,所以解决你的问题。

public static void setPhoneNumberFilter(final EditText edt, final int length){ InputFilter[] filters = new InputFilter[1];

    final int len = edt.getText().toString().length();
Log.e("MIS", "" + edt.getText().toString());
Log.e("MIS", "" + edt.getText().toString().length());
if (len > length - 1) {
return;
}
filters[0] = new InputFilter() {

public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// TODO Auto-generated method stub

try {
char[] acceptedChars = new char[] { '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9' };
for (int index = 0; index < end; index++) {

if (!new String(acceptedChars).contains(String
.valueOf(source.charAt(index)))
|| edt.getText().toString().length() > length - 1) {
return "";
}
}

} catch (Exception e) {
e.printStackTrace();

}

return null;
}

};

edt.setFilters(filters);
}

setPhoneNumberFilter(edtxt,10);

关于Android 对 edittext 的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com