2011-01-06 75 views
0

我想限制用戶在編輯文本中放置超過4個句點(。)。在運行時計算一個特定的字符在android

如何做到這一點。請任何身體幫助

+0

連續4次或總共4次?你已經有了一個可以讀取其內容的`EditText`和`Activity`的佈局嗎? – 2011-01-06 10:57:51

回答

2

請使用下面的代碼。

public class Help extends Activity { 
public static int count = 0;//use this to check is there are more that 4 '.' char 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ((EditText)findViewById(R.id.EditText01)).addTextChangedListener(new TextWatcher() { 


     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      if(count>=4){ 
       //don't allow to right text 
      } 

     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      //check here if entered text contains more than 4 '.' character 

     } 



     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 

    }); 
} 

請檢查我沒有測試過

+0

thnx ..求助 – 2011-01-06 13:14:39

0
Edittext e = (editText)Findviewbyid..... 

String t= e.geteditable().gettext(); // check methods 
if(s.equals(".") || s.equals("..") || s.equals("...") 


{ 
     throw some exception and reset it in the catch block 
    } 

我能理解你的問題。這是你想要的嗎?

0

下面的腳本邏輯計算各個時期,但多個週期計算一次:... = .,並在一開始preriods不計算在內。

String text = ((EditText)findViewById(R.id.yourEditText)).getText().toString(); 
if(text.matches("\\.*[^\\.]+\\.+[^\\.]+\\.+[^\\.]+\\.+[^\\.]+\\.+.+")){ 
    // 4 or more '.', multiple '..' are counted once 
}