开发者

EditText in Google Android

I want to create the EditText which allow Numeric Values, Comma and Del开发者_StackOverflow中文版ete and other values are ignore.

So How I can achieve this by programming code ?

Thanks.


I achieved same thing using follwing code, hope you will also find help from it.

editText.addTextChangedListener(controller);


@Override
public void afterTextChanged(Editable s) {
    if(s.trim.length() > 0)
            {
                 int start = s.toString().length() - 1;
                 int a = (int) s.charAt(start);
                 if (!(a >= 48 && a <= 57 || a == 44))
                    s = s.delete(start, start + 1);
            }
}

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

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}


To restrict what characters can be typed into an android EditText, you must set them in the android:digits XML attribute. See http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits . Make sure to also validate user input before putting it into storage.


just simple ,

you want to create a text-field like that you can enter the new string must have some grammar that you want to allow.

i have some email validation grammar that can help you.

string = "/alphabetics/number/special-character" ;

and another is

user-entered-string = " ";

just compare user-entered-string's lexems and you defined grammar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜