我想限制用户在编辑文本中放置超过4个句点(。)。
如何做到这一点。请任何身体帮助
答案 0 :(得分: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
}
});
}
请检查我未测试的逻辑
答案 1 :(得分: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
}
我可以从你的问题中理解这一点。这是你想要的吗?
答案 2 :(得分:0)
以下脚本统计所有期间,但多个期间计算一次:...
= .
,并且不计算开头的预售。
String text = ((EditText)findViewById(R.id.yourEditText)).getText().toString();
if(text.matches("\\.*[^\\.]+\\.+[^\\.]+\\.+[^\\.]+\\.+[^\\.]+\\.+.+")){
// 4 or more '.', multiple '..' are counted once
}