2011-01-06 2 views
0

Я хочу ограничить пользователя помещением более 4 периодов (.) В текст редактирования.подсчет определенного символа во время выполнения в android

как сделать. пожалуйста, любой орган помощи

+0

4 в строке или 4 в общей сложности? У вас уже есть макет с «EditText» и «Activity», который может читать его содержимое? –

ответ

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 .. для справки –

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 
} 
Смежные вопросы