2010年10月6日水曜日

Android Filterを使ってみた

android.widget.Filter を使ってみた。

例えば、EditText で英数字しか入力できないようにするには
こんな感じにする。

  1. public class FilterTest extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.           
  8.         EditText ed = (EditText)findViewById(R.id.edittext);  
  9.         ed.setFilters(filters);  
  10.     }  
  11.       
  12.     private InputFilter[] filters = { new MyFilter() };  
  13.   
  14.     class MyFilter implements InputFilter {  
  15.         public CharSequence filter(CharSequence source, int start, int end,  
  16.                                    Spanned dest, int dstart, int dend) {  
  17.         
  18.             if( source.toString().matches("^[a-zA-Z0-9]+$") ){  
  19.                 return source;  
  20.             }else{  
  21.                 return "";  
  22.             }  
  23.         }  
  24.     }  
  25. }  


filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend)
の各引数について

source: 今入力されていて、確定していない文字列
     つまり、一般的に下線がでている変換前の文字列

start : 今入力されていて、確定していない文字列の先頭位置
     常に 0 になる(みたいだ)

end  : 今入力されていて、確定していない文字列の終端位置
     常に確定していない文字列の長さになる(みたいだ)

dest : 今EditTextに入っている文字列、確定していない文字列も含む

dstart : 今入力されていて確定していない文字列の、 EditText
     に入っている文字列上での先頭位置
     
dend : 今入力されていて確定していない文字列の、 EditText に
    入っている文字列上での終端位置

試しにログを出してみた。

  1. --- a ---  
  2.   
  3. D/filterTest(14321): source : a  
  4. D/filterTest(14321): start : 0  
  5. D/filterTest(14321): end : 1  
  6. D/filterTest(14321): dest :   
  7. D/filterTest(14321): dstart : 0  
  8. D/filterTest(14321): dend : 0  
  9. D/filterTest(14321): source : a  
  10. D/filterTest(14321): start : 0  
  11. D/filterTest(14321): end : 1  
  12. D/filterTest(14321): dest : a  
  13. D/filterTest(14321): dstart : 0  
  14. D/filterTest(14321): dend : 1  
  15.   
  16. --- b ---  
  17.   
  18. D/filterTest(14321): source : ab  
  19. D/filterTest(14321): start : 0  
  20. D/filterTest(14321): end : 2  
  21. D/filterTest(14321): dest : a  
  22. D/filterTest(14321): dstart : 0  
  23. D/filterTest(14321): dend : 1  
  24. D/filterTest(14321): source : ab  
  25. D/filterTest(14321): start : 0  
  26. D/filterTest(14321): end : 2  
  27. D/filterTest(14321): dest : ab  
  28. D/filterTest(14321): dstart : 0  
  29. D/filterTest(14321): dend : 2  
  30.   
  31. --- c ---  
  32.   
  33. D/filterTest(14321): source : abc  
  34. D/filterTest(14321): start : 0  
  35. D/filterTest(14321): end : 3  
  36. D/filterTest(14321): dest : ab  
  37. D/filterTest(14321): dstart : 0  
  38. D/filterTest(14321): dend : 2  
  39. D/filterTest(14321): source : abc  
  40. D/filterTest(14321): start : 0  
  41. D/filterTest(14321): end : 3  
  42. D/filterTest(14321): dest : abc  
  43. D/filterTest(14321): dstart : 0  
  44. D/filterTest(14321): dend : 3  
  45.   
  46. --- Enter ---  
  47.   
  48. D/filterTest(14321): source : abc  
  49. D/filterTest(14321): start : 0  
  50. D/filterTest(14321): end : 3  
  51. D/filterTest(14321): dest : abc  
  52. D/filterTest(14321): dstart : 0  
  53. D/filterTest(14321): dend : 3  
  54. D/filterTest(14321): source :    
  55. D/filterTest(14321): start : 0  
  56. D/filterTest(14321): end : 1  
  57. D/filterTest(14321): dest : abc  
  58. D/filterTest(14321): dstart : 3  
  59. D/filterTest(14321): dend : 3  
  60.   
  61. --- あ ---  
  62.   
  63. D/filterTest(14321): source : あ  
  64. D/filterTest(14321): start : 0  
  65. D/filterTest(14321): end : 1  
  66. D/filterTest(14321): dest : abc  
  67. D/filterTest(14321): dstart : 3  
  68. D/filterTest(14321): dend : 3  
  69.   
  70. --- い ---  
  71.   
  72. D/filterTest(14321): source : あい  
  73. D/filterTest(14321): start : 0  
  74. D/filterTest(14321): end : 2  
  75. D/filterTest(14321): dest : abc  
  76. D/filterTest(14321): dstart : 3  
  77. D/filterTest(14321): dend : 3  
  78.   
  79. --- う ---  
  80.   
  81. D/filterTest(14321): source : あいう  
  82. D/filterTest(14321): start : 0  
  83. D/filterTest(14321): end : 3  
  84. D/filterTest(14321): dest : abc  
  85. D/filterTest(14321): dstart : 3  
  86. D/filterTest(14321): dend : 3  
  87.   
  88. --- Enter ---  
  89.   
  90. D/filterTest(14321): source : あいう  
  91. D/filterTest(14321): start : 0  
  92. D/filterTest(14321): end : 3  
  93. D/filterTest(14321): dest : abc  
  94. D/filterTest(14321): dstart : 3  
  95. D/filterTest(14321): dend : 3  
  96. D/filterTest(14321): source :   
  97. D/filterTest(14321): start : 0  
  98. D/filterTest(14321): end : 0  
  99. D/filterTest(14321): dest : abc  
  100. D/filterTest(14321): dstart : 3  
  101. D/filterTest(14321): dend : 3  
  102.   
  103. --- 1 ---  
  104.   
  105. D/filterTest(14321): source : 1  
  106. D/filterTest(14321): start : 0  
  107. D/filterTest(14321): end : 1  
  108. D/filterTest(14321): dest : abc  
  109. D/filterTest(14321): dstart : 3  
  110. D/filterTest(14321): dend : 3  
  111.   
  112. --- 2 ---  
  113.   
  114. D/filterTest(14321): source : 2  
  115. D/filterTest(14321): start : 0  
  116. D/filterTest(14321): end : 1  
  117. D/filterTest(14321): dest : abc1  
  118. D/filterTest(14321): dstart : 4  
  119. D/filterTest(14321): dend : 4  
  120.   
  121. --- 3 ---  
  122.   
  123. D/filterTest(14321): source : 3  
  124. D/filterTest(14321): start : 0  
  125. D/filterTest(14321): end : 1  
  126. D/filterTest(14321): dest : abc12  
  127. D/filterTest(14321): dstart : 5  
  128. D/filterTest(14321): dend : 5  



 

0 件のコメント:

コメントを投稿