Reputation: 237
I want to limit input in the EditText of all symbols except figures, "?" And "*". How I am able to do it?
Upvotes: 8
Views: 15827
Reputation: 689
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 });
}
}
Upvotes: 0
Reputation: 76
Try this
From this code you can enter 0 to 9 digits and only two special character ? and *.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/first_name"
android:digits="?*1234567890"
android:singleLine="true"
/>
Upvotes: 0
Reputation: 385
you can check your input from edittext.addTextChangeListener
method.
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 onTextChanged
method you can restrict the user to maximum length
and also you can check the input type
according to your requirement
Upvotes: 0
Reputation: 273
By this, we can restrict the user to not type ? and *
<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"/>
Upvotes: 0
Reputation: 1302
If you also have a maximum strict length of your string, you can use a mask. I'm not sure that it's the right solution, but it's the easiest one. An easy way use a mask in your Android programs in Android Studio is to use MaskedEditText library (GitHub link).
I can not understand what does mean: figures, "?" And "", so I suppose just "?" and "". So the code is:
In your build.gradle:
compile 'ru.egslava:MaskedEditText:1.0.5'
And in your layout:
<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"
/>
This code creates an EditText that can contain ONLY "?" and "*" and max length is 10. Feel free to ask your questions :-)
Upvotes: 1
Reputation: 77752
You could add a TextWatch
via addTextChangedListener
. afterTextChanged
allows you to modify the text after it was modified.
(Keep in mind that there are many ways to modify a text field - entering text, auto-complete through IME, copy and paste). Handle those properly in the TextWatch.
Upvotes: 0