例えば、EditText で英数字しか入力できないようにするには
こんな感じにする。
- public class FilterTest extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- EditText ed = (EditText)findViewById(R.id.edittext);
- ed.setFilters(filters);
- }
- private InputFilter[] filters = { new MyFilter() };
- class MyFilter implements InputFilter {
- public CharSequence filter(CharSequence source, int start, int end,
- Spanned dest, int dstart, int dend) {
- if( source.toString().matches("^[a-zA-Z0-9]+$") ){
- return source;
- }else{
- return "";
- }
- }
- }
- }
filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend)
の各引数について
source: 今入力されていて、確定していない文字列
つまり、一般的に下線がでている変換前の文字列
start : 今入力されていて、確定していない文字列の先頭位置
常に 0 になる(みたいだ)
end : 今入力されていて、確定していない文字列の終端位置
常に確定していない文字列の長さになる(みたいだ)
dest : 今EditTextに入っている文字列、確定していない文字列も含む
dstart : 今入力されていて確定していない文字列の、 EditText
に入っている文字列上での先頭位置
dend : 今入力されていて確定していない文字列の、 EditText に
入っている文字列上での終端位置
試しにログを出してみた。
- --- a ---
- D/filterTest(14321): source : a
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 1
- D/filterTest(14321): dest :
- D/filterTest(14321): dstart : 0
- D/filterTest(14321): dend : 0
- D/filterTest(14321): source : a
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 1
- D/filterTest(14321): dest : a
- D/filterTest(14321): dstart : 0
- D/filterTest(14321): dend : 1
- --- b ---
- D/filterTest(14321): source : ab
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 2
- D/filterTest(14321): dest : a
- D/filterTest(14321): dstart : 0
- D/filterTest(14321): dend : 1
- D/filterTest(14321): source : ab
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 2
- D/filterTest(14321): dest : ab
- D/filterTest(14321): dstart : 0
- D/filterTest(14321): dend : 2
- --- c ---
- D/filterTest(14321): source : abc
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 3
- D/filterTest(14321): dest : ab
- D/filterTest(14321): dstart : 0
- D/filterTest(14321): dend : 2
- D/filterTest(14321): source : abc
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 3
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 0
- D/filterTest(14321): dend : 3
- --- Enter ---
- D/filterTest(14321): source : abc
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 3
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 0
- D/filterTest(14321): dend : 3
- D/filterTest(14321): source :
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 1
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 3
- D/filterTest(14321): dend : 3
- --- あ ---
- D/filterTest(14321): source : あ
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 1
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 3
- D/filterTest(14321): dend : 3
- --- い ---
- D/filterTest(14321): source : あい
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 2
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 3
- D/filterTest(14321): dend : 3
- --- う ---
- D/filterTest(14321): source : あいう
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 3
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 3
- D/filterTest(14321): dend : 3
- --- Enter ---
- D/filterTest(14321): source : あいう
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 3
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 3
- D/filterTest(14321): dend : 3
- D/filterTest(14321): source :
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 0
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 3
- D/filterTest(14321): dend : 3
- --- 1 ---
- D/filterTest(14321): source : 1
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 1
- D/filterTest(14321): dest : abc
- D/filterTest(14321): dstart : 3
- D/filterTest(14321): dend : 3
- --- 2 ---
- D/filterTest(14321): source : 2
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 1
- D/filterTest(14321): dest : abc1
- D/filterTest(14321): dstart : 4
- D/filterTest(14321): dend : 4
- --- 3 ---
- D/filterTest(14321): source : 3
- D/filterTest(14321): start : 0
- D/filterTest(14321): end : 1
- D/filterTest(14321): dest : abc12
- D/filterTest(14321): dstart : 5
- D/filterTest(14321): dend : 5
0 件のコメント:
コメントを投稿