Mask for EditText in Android

I want to limit the input to EditText of all characters except numbers, "?" And "*". How can i do this?

+8
source share
7 answers

You can add TextWatchvia addTextChangedListener. afterTextChangedallows you to change the text after changing it.

(Keep in mind that there are many ways to change the text box - text input, auto-completion via IME, copy and paste). Handle them correctly in TextWatch.

0
source

Edited by mistake ... Sorry.

0
source

, . , , . Android Android Studio - MaskedEditText ( GitHub).

, : , "?" "", , "?" "". , :

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
source

You can check your input from a method 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) {

        }
    });

Inside the method onTextChangedyou can restrict the user to maximum lengthand you can also check input typeaccording to your requirements

0
source
public class MainActivity extends Activity {

private EditText editText;
private String blockCharacterSet = "?*";

private InputFilter filter = new InputFilter() {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        if (source != null && !blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText);
    editText.setFilters(new InputFilter[] { filter });
}

}

0
source

Try it

From this code, can you enter from 0 to 9 digits and only two special characters? and *.

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/first_name"
   android:digits="?*1234567890"
   android:singleLine="true"
   />
-1
source

All Articles