By example, i have an input filter, which block any characters in edititext:
editText.setFilters(new InputFilter[]{new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return "";
}
}});
But autosuggestions continue updating, so user can click on thems or press back button and edittext will be filled with filtered data.
Hоw to avoid this strange logic? (Do not change autosuggestions, when characher was filtered) Tested on huawei ascend p6.
Related
Have been trying to implement a way for the soft keyboard to open first the Numeric key without saying that the EditText is numeric.
Is this possible ? if yes how ?
The editText btw are added dynamic, so can't do changes on XML files
You can set input type programmatically.
EditText editText=new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Edit
You can use InputFilters
public class NumericInputFilter implements InputFilter {
String regEx = new String("^[0-9]+$")
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
int dend) {
if (source != null && !source.toString().matches(regEx) && !source.toString().equals( "")) {
return "";
}
return source;
}
and on your editText, editText.setFilters(new InputFilter[]{new NumericInputFilter()});
If we set the editText such as editTextField.setRawInputType(Configuration.KEYBOARD_QWERTY);
it will open a keyboard with numeric first and let me choose the normal one and input letters also.
I have an EditText in which i want to allow only alphabets and numbers of any language. I tried with different android:inputType and android:digits in XML.
I tried with set TextWatcher to edittext in which onTextChanged() is like
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
switch (et.getId()) {
case R.id.edtMultiLang: {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]0-9 ]", ""));
// et.setText(s.toString().replaceAll("[^A-Za-z0-9 ]", ""));
et.addTextChangedListener(watcher2);
break;
}
}
}
This is working fine. But whenever i tried to clear text, cursor is moving to start for every letter. Means when i clear a single letter, cursor moving to start.
If i use like android:digits="abcdefghijklmnopqrstuvwxyz1234567890 ", it allows me to type only alphabets and numbers of english. It is not allowing me to enter any other language text As i given only English related alphabets here. But my requirement is to allow copy/paste of other language's alphabets and letters also.
I hope we can do this by using Patterns, TextWatcher and InputFilter. But i didn't find better way.
Please let me know if there any way to do this.
The option you mention to resolve the problem is easy and fast, if you use a filter your code will be like this:
public static InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String blockCharacterSet = "~#^|$%*!#/()-'\":;,?{}=!$^';,?×÷<>{}€£¥₩%~`¤♡♥_|《》¡¿°•○●□■◇◆♧♣▲▼▶◀↑↓←→☆★▪:-);-):-D:-(:'(:O 1234567890";
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });
Can i detect characters the user is typing before user's touch selection on the Chinese word?
iOS can achieve this.
This is Android, the EditText TextWatcher will get nothing before user selection.
Read the official doc here.
By using a TextWatcher, you'll have access to the afterTextChanged(), beforeTextChanged() and onTextChanged() methods. Now you can see what pinyin the user enters as he enters it and then change your hanzi suggestions as his input changes.
Try this way
InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
//source : latest character typed in edittext
//dest : all characters except source
// HERE YOU WILL GET CALLBACK EVERY TIME key ENTERED IN EDITTEXT
return null;
}
};
edtText.setFilters(new InputFilter[]{filter});
I've a multiline text box in my app and i want to set a fixed character length so that in one line not more than of that characters appear how to do this?
say if maxlegnth is 150 for textbox then maxlength for single line is to set 50. MAX LENGTH FOR EACH LINE
For TextView elements, or any subclsses of TextView(such as EditText), you can manage this with setFilters().
What you need to do is to create a class implementing InputFilter, for example
public class MyTestFilter implements InputFilter {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// do you checks, and only return what you wish to have replaced
return null;
}
}
and then use
youEditText.setFilters(new InputFilter[]{new MyTestFilter()});
I am writing an application on Android and I have these bunch of buttons; when you click on them the input panel (EditText) appears if it is not already visible otherwise it disappears.
The problem is that the user can still type into the EditText even after it's disappeared. I've attached two pictures to show what I mean.
I tried .setEnabled(false) - didn't work.
I tried .setFocusable(false) - didn't work.
I tried both together - didn't work.
You can try hide soft keyboard before setting visibility.
InputMethodManager imm = (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editView.getWindowToken(), 0);
I think you need to close Android soft keyboard. This link will help you with this issue: Close/hide the Android Soft Keyboard
you can use TextWatcher alongwith flag http://developer.android.com/reference/android/text/TextWatcher.html
prepare logic such that once visibilty gone then in public void onTextChanged(CharSequence s, int start, int before, int count) put same Text as earlier
refer this too Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged
These answers to these questions say you can use an InputFilter
editText.setFilters(new InputFilter[] {
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
}
});
Why can I type into a disabled EditText?
Can we have uneditable text in edittext