添加输入约束编辑文本在Android中? [英] Add input constraints to Edit Text in Android?

查看:172
本文介绍了添加输入约束编辑文本在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它需要在应用程序中输入一些数值的条件。

I have condition where it is required to enter some numeric values in the application.

案例:


  1. 如果用户输入整数那么最大长度应为8位数字。例如:12345678

  1. If user enters whole number then the max length should be 8 digits. Eg: 12345678

如果用户想要输入十进制值,他只能高达2小数点输入。
  例如:1.03,123.95

If users wants to enter decimal value he can only input upto 2 decimal points. Eg: 1.03, 123.95

如果用户输入整个8位那么他也可以输入小数高达2小数点。例如:12345678.12

If users enters whole 8 digits then he can also input the decimal places upto 2 decimal points. Eg: 12345678.12

因此​​,我们有以下的有效输入:

So we have the following valid inputs:

     1.  12345678
     2.  123.45
     3.  12345678.98

我怎样才能实现上述情况?

How can I achieve the above case scenario?

推荐答案

在这里你去。这正是你想要的...

Here you go. This is exactly what you want...

    import android.app.Activity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.InputFilter;
    import android.text.Spanned;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.widget.EditText;

    public class EditTextLogic extends Activity
    {
        EditText mEditText;
        String TEMP="",INPUT="";
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.edit_tesxt_logic);  
            mEditText =(EditText) findViewById(R.id.textView1);

            mEditText.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {}

                @Override
                public void afterTextChanged(Editable Token) 
                {
                    if(Token.toString().length()<INPUT.length())
                    {
                        INPUT=Token.toString();
                    }
                }
            });
            mEditText.setFilters(new InputFilter[] {
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence input, int arg1,
                                int arg2, Spanned arg3, int arg4, int arg5) 
                        {
                            CharSequence returned = validate(input.toString());
                            return returned;
                        }
                    }
            });

        }

        private CharSequence validate(String Token) {
            Log.i("Token",""+Token);
            TEMP = INPUT;
            TEMP += Token; 
            if(TEMP.contains("."))
            {
                try
                {
                    String FractionNo = TEMP.split("\\.")[1];
                    if(FractionNo.length()> 2)
                    {

                        return "";
                    }
                    else
                    {
                        INPUT = TEMP;
                        return Token;
                    }
                }
                catch(ArrayIndexOutOfBoundsException e)
                {
                    INPUT = TEMP;
                    return Token;
                }
            }
            else
            {
                if(TEMP.length()>8)
                {
                    return "";
                }
                else
                {
                    INPUT = TEMP;
                    return Token;
                }
            }
        }
    }

这是你的XML:

And this is your XML:

<EditText
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:singleLine="true"/>

干杯!
享受编码!

Cheers! Enjoy Coding!!

这篇关于添加输入约束编辑文本在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆