Android中EditText的掩码

问题描述 投票:8回答:5

我想限制除了数字之外的所有符号的EditText中的输入,“?”和“*”。我怎么能这样做?

android android-edittext mask
5个回答
0
投票

你可以通过TextWatch添加一个addTextChangedListenerafterTextChanged允许您在修改后修改文本。

(请记住,有很多方法可以修改文本字段 - 输入文本,通过IME自动完成,复制和粘贴)。在TextWatch中正确处理这些。


0
投票

您可以使用此方法:

EditText editText = new EditText(this);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

然后在beforeTextChanged方法中防止插入“?”和“*”数字。


0
投票

如果你的字符串也有最大严格长度,你可以使用掩码。我不确定这是正确的解决方案,但它是最简单的解决方案。在Android Studio的Android程序中使用蒙版的一种简单方法是使用MaskedEditText库(GitHub link)。

我无法理解什么意思:数字,“?”和“”,所以我想只是“?”和“”。所以代码是:

在build.gradle中:

compile 'ru.egslava:MaskedEditText:1.0.5'

在你的布局中:

<br.com.sapereaude.maskedEditText.MaskedEditText
    android:id="@+id/phone_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:typeface="monospace"
    mask:allowed_chars="?*"
    mask:mask="##########"
    app:keep_hint="true"
    />

此代码创建一个只能包含“?”的EditText。和“*”和最大长度是10.随意问你的问题:-)


0
投票

这样,我们可以限制用户不输入?和*

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textSize="@dimen/register_text_size"
   android:layout_weight="1"
   android:id="@+id/first_name"
   android:padding="14dp"
   android:digits="@#$%^&()_-+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12"
   android:singleLine="true"
   android:hint="@string/first_name"/>

0
投票

你可以从edittext.addTextChangeListener方法检查你的输入。

etAdjustmentInput.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {             
             //checking the length of input 
            if (etAdjustmentInput.getText().toString().trim().length()<=0){
                 // if length less then or equals 0 show warning or make button not clickable
                tvAdjustmentInfoOk.setClickable(false);
                tvAdjustmentInfoOk.setTextColor(getResources().getColor(R.color.time_color));
            }
            else {

                 // input length greater then 0 now check the input type according to your requirement 
                tvAdjustmentInfoOk.setClickable(true);
                tvAdjustmentInfoOk.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                if(s.equals("?")){
                 //do your task
                  }
                else{ // whatever you want to tell the user 
                  }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

onTextChanged方法内你可以限制用户maximum length,你也可以根据你的要求检查input type

© www.soinside.com 2019 - 2024. All rights reserved.