1. 程式人生 > >在解決密碼輸入框裡不能輸入中文以及空格時所找到的解決方案

在解決密碼輸入框裡不能輸入中文以及空格時所找到的解決方案

1.原理說明,中文佔用2個字元,以此來過濾

一般在監聽輸入可以使用TextWatcher這個監視器,但是動態輸入還使用它的話就會出現bug,因為Watcher觀察者的3個函式都不是那麼好使用,使得輸入緩衝區有中文不會顯示在輸入框中,即使使用截斷字元創的方式來控制顯示,但是如果輸入了一串英文字元後,再將游標定位在英文字串中進行輸入中文,bug就出現了。這個時候就必須使用filter來過濾了。

廢話少說,看程式碼:

情景:如果有中文字元的話,輸入框只能輸入固定字元比如說6個,如果全英文字元,可以輸入20個,如果超過6個字元,想輸入中文的話,就將中文過濾。

package cn.edu.wtu;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;
import android.widget.EditText;

public class InputFilterActivity extends Activity {
    /** Called when the activity is first created. */
    private static final String TAG = "InputFilterActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText edit =  (EditText)findViewById(R.id.editText1);
        edit.setText("moon");
        edit.setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(20),
                new InputFilter(){
                    
                    @Override
                    public CharSequence filter(CharSequence source, int start,
                            int end, Spanned dest, int dstart, int dend) {
                        // TODO Auto-generated method stub
                        Log.d(TAG, source.toString()+"\tstart:"+start+"\tend:"+end);
                        Log.d(TAG, dest.toString()+"\tdstart:"+dstart+"\tdend:"+dend);
                        if(isCN(source.toString())){
                            if(dstart >= 6){
                                edit.setSelection(dstart);
                                return "";
                            }else{
                                if(source.length()+dest.length() > 6){
                                    return source.subSequence(start, start+6-dest.length()); 
                                }else{
                                    return source;
                                }
                            }
                        }else{
                            return source;
                        }
                    }
                    
                }
        });
    }
    
    public boolean isCN(String str){
        try {
            byte [] bytes = str.getBytes("UTF-8");
            if(bytes.length == str.length()){
                return false;
            }else{
                return true;
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}

xml檔案就不貼了,很簡單,因為中文的UNICODE編碼是2個位元組,所以可以利用這個特性判斷是否為中文字元

原文連結http://blog.csdn.net/fuuckwtu/article/details/6799904

2.利用IntputFilter

public static void inputFilterSpace(final EditText edit){
        edit.setFilters(new InputFilter[]
                                           {
                                            new InputFilter.LengthFilter(16),


                                               new InputFilter(){    
                                                   public CharSequence filter(CharSequence src, int start, int end, Spanned dst, int dstart, int dend) {   
                                                       Log.d(TAG,"src:"+src+";start:"+start+";end:"+end);   

                                                       Log.d(TAG,"dest:"+dst+";dstart:"+dstart+";dend:"+dend); 
                                                       if(src.length()<1)
                                                       {
                                                            return null;
                                                       }
                                                       else
                                                       {
                                                           char temp [] = (src.toString()).toCharArray();
                                                           char result [] = new char[temp.length];
                                                           for(int i = 0,j=0; i< temp.length; i++){
                                                               if(temp[i] == ' '){
                                                                   continue;
                                                               }else{
                                                                   result[j++] = temp[i];
                                                               }
                                                           }
                                                           return String.valueOf(result).trim();
                                                       }

                                                   }
                                               }          
                                             });    
    }

原文連結:http://blog.csdn.net/fuuckwtu/article/details/6968766

3.android同時控制EditText輸入字元個數和禁止特殊字元輸入的方法,涉及Android操作EditText控制字元操作的技巧

intmMaxLenth = 200;//設定允許輸入的字元長度 publicstaticString stringFilter(String str)throwsPatternSyntaxException{ String regEx = "[/\\:*?<>|\"\n\t]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); returnm.replaceAll(""); } mEditText.addTextChangedListener(newTextWatcher() { privateintcou = 0; intselectionEnd = 0; @Override publicvoidonTextChanged(CharSequence s, intstart,intbefore, intcount) { cou = before + count; String editable = mEditText.getText().toString(); String str = stringFilter(editable); //過濾特殊字元 if(!editable.equals(str)) { mEditText.setText(str); } mEditText.setSelection(mEditText.length()); cou = mEditText.length(); } @Override publicvoidbeforeTextChanged(CharSequence s, intstart,intcount, intafter) { } @Override publicvoidafterTextChanged(Editable s) { if(cou > mMaxLenth) { selectionEnd = mEditText.getSelectionEnd(); s.delete(mMaxLenth, selectionEnd); } } });
原文連結:http://www.jb51.net/article/64721.htm

總結:InputFilter需要重點了解下,用例範圍應該很廣,如有更好的辦法,還希望各位博友多多指教!