2013-05-15 30 views
1

我需要設置一些限制EditText.Only的輸入允許用戶輸入a-zA-Z0-9semicolon ;chinese lettersAndroid的限制EDITTEXT

我的佈局非常簡單,一個edittext和一個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 = "abcdefghijklmnopqrstuvwxyz;"; 
     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) { 
       } 
     }; 
} 
+0

看看這些帖子: http://stackoverflow.com/a/4401227/967548和http://stackoverflow.com/a/10826771/967548 –

回答

1

波紋管代碼只接受0到9位數,所以解決你的問題。

public static void setPhoneNumberFilter(final EditText edt,final int length) InputFilter [] filters = new InputFilter [1];

final int len = edt.getText().toString().length(); 
    Log.e("MIS", "" + edt.getText().toString()); 
    Log.e("MIS", "" + edt.getText().toString().length()); 
    if (len > length - 1) { 
     return; 
    } 
    filters[0] = new InputFilter() { 

     public CharSequence filter(CharSequence source, int start, int end, 
       Spanned dest, int dstart, int dend) { 
      // TODO Auto-generated method stub 

      try { 
       char[] acceptedChars = new char[] { '0', '1', '2', '3', 
         '4', '5', '6', '7', '8', '9' }; 
       for (int index = 0; index < end; index++) { 

        if (!new String(acceptedChars).contains(String 
          .valueOf(source.charAt(index))) 
          || edt.getText().toString().length() > length - 1) { 
         return ""; 
        } 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 

      } 

      return null; 
     } 

    }; 

    edt.setFilters(filters); 
} 

setPhoneNumberFilter(edtxt,10);