每个逗号限制为android中的六个字符

时间:2012-02-01 12:03:34

标签: java android

我需要在android的edittext中用逗号限制六个字符,我应该能够为逗号添加字符...

即3000,898989,898345 ... 3000可以是30000或300000 ...无限但逗号后的字符应该固定为6 ...

在每个逗号后,只能在编辑文本中键入六个字符...

如何去做?

我尝试了以下

public class CustomEditTextActivity extends Activity {

EditText et;
Context cn=null;
int commacount=0;
String aftercomma;
boolean  flag=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
                             cn=this;
    et=(EditText)findViewById(R.id.editText1);

    // Listener for edit text text change
    et.addTextChangedListener(filterTextWatcher);


}

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

        System.out.println("Inside afterTextChanged() method");



    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

        System.out.println("Inside beforeTextChanged() method");


    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
                      flag=false;
        commacount=0;
        String getDataFromeditxt = s.toString();
                int comma = getDataFromeditxt.indexOf(',');
                String bforcomma = getDataFromeditxt;
                if (comma != -1)
                {
                    bforcomma = getDataFromeditxt.substring(0, comma);
                }



                               // Checking for , in the data and setting its position
                                for (int i = 0; i < getDataFromeditxt.length(); i++) {
                                    char c = getDataFromeditxt.charAt(i);
                                     if(flag){
                                      aftercomma=new StringBuilder().append(c).toString();
                                         int maxLength =bforcomma.length() +6+commacount;
                                        InputFilter[] FilterArray = new InputFilter[1];
                                        FilterArray[0] = new InputFilter.LengthFilter(maxLength);
                                        et.setFilters(FilterArray);
                                        System.out.println(aftercomma);

                                    }
                                    if (c == ',') {
                                        commacount=commacount+1 ;
                                         flag=true;
                                    }

                                }

    }

};

}

上面代码中的问题是,一旦设置了maxlength,我就无法输入任何...任何解决方案??

1 个答案:

答案 0 :(得分:0)

Use pattern matching in java. Replace str with text from editext.
String result="";
String str= "300066666666666666666,898989,898345,000000,111111,333333";//editext.getText().toString();
String regex ="(^[0-9]+)([,]+[0-9]{6})+$";
Matcher matcher = Pattern.compile( regex ).matcher( str);
if (matcher.find( ))
{
result = matcher.group();  
System.out.println("number="+result);            
}
else
{
     System.out.println("no match found"); 
}
output
number=300066666666666666666,898989,898345,000000,111111,333333