2011-01-06 48 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 
}