注:(标题有改动)原文出自:http://wang-peng1.javaeye.com/blog/669520 和 http://wang-peng1.javaeye.com/blog/669384
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Class which shows how to lock and unlock EditText component
*
* @author FaYnaSoft Labs
*/
public class Main extends Activity {
private EditText editText;
private boolean value = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.textId);
editText.setText(“EditText component”);
Button b = (Button) findViewById(R.id.btnId);
b.setText(“Lock/Unlock”);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (value) {
value = false;
} else {
value = true;
}
lockUnlock(value);
}
});
}
/**
* Method which locks and unlocks editText component
* @param value our boolean value which using in or if operator
*/
private void lockUnlock(boolean value) {
if (value) {
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
return source.length() < 1 ? dest.subSequence(dstart, dend)
: “”;
}
} });
} else {
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
return null;
}
} });
}
}
}
二 。 在edittext中过滤Filtering,可用的数据
emailText.setKeyListener(new NumberKeyListener() {
@Override
public int getInputType() {
return InputType.TYPE_MASK_VARIATION;
}
@Override
protected char[] getAcceptedChars() {
return new char[]{‘i’, ‘o’, ‘p’, ‘a’, ‘s’, ‘d’, ‘f’, ‘g’,
‘h’, ‘j’, ‘k’, ‘l’, ‘z’, ‘x’, ‘q’, ‘w’, ‘e’, ‘r’, ‘t’,
‘y’, ‘u’, ‘c’, ‘v’, ‘b’, ‘n’, ‘m’, ‘.’, ‘@’, ‘_’, ’1′, ’2′,
’3′, ’4′, ’5′, ’6′, ’7′, ’8′, ’9′, ’0′,};
}
});
——————————————————————————-
emailText.setKeyListener(new NumberKeyListener() {
@Override
public int getInputType() {
return InputType.TYPE_NULL;
}
@Override
protected char[] getAcceptedChars() {
return new char[]{‘i’, ‘o’, ‘p’, ‘a’, ‘s’, ‘d’, ‘f’, ‘g’,
‘h’, ‘j’, ‘k’, ‘l’, ‘z’, ‘x’, ‘q’, ‘w’, ‘e’, ‘r’, ‘t’,
‘y’, ‘u’, ‘c’, ‘v’, ‘b’, ‘n’, ‘m’, ‘.’, ‘@’, ‘_’, ’1′, ’2′,
’3′, ’4′, ’5′, ’6′, ’7′, ’8′, ’9′, ’0′,};
}
});
………………………………………………………………………….
emailText.setText(“EditText element”);
emailText.setKeyListener(new NumberKeyListener() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
return source.length() < 1 ? dest.subSequence(dstart, dend) : “”;
}
@Override
public int getInputType() {
return InputType.TYPE_MASK_VARIATION;
}
@Override
protected char[] getAcceptedChars() {
return new char[]{‘i’, ‘o’, ‘p’, ‘a’, ‘s’, ‘d’, ‘f’, ‘g’,
‘h’, ‘j’, ‘k’, ‘l’, ‘z’, ‘x’, ‘q’, ‘w’, ‘e’, ‘r’, ‘t’,
‘y’, ‘u’, ‘c’, ‘v’, ‘b’, ‘n’, ‘m’, ‘.’, ‘@’, ‘_’, ’1′, ’2′,
’3′, ’4′, ’5′, ’6′, ’7′, ’8′, ’9′, ’0′,};
}
});