EditText の入力にいくつかの制限を設定する必要がありますa-z
。
ユーザーはA-Z
、、、、および.0-9
semicolon ;
chinese letters
私のレイアウトは非常にシンプルで、1 つの edittext と 1 つの 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) {
}
};
}