Android has a lot of input method applications, each input method has its own characteristics, input method Most of the time is and edittext with the use, combined with my own hands-on experience to share some of the problems encountered in the process of edittext and solutions.
Set Default IME
Sometimes in order to improve the user experience, in the popup input method needs to set the default input state, such as the Word application pop-up input method, the input method is preferably in the English input state. If it is a dictionary application, the best way to pop up the IME is in the Chinese input state, Android does not provide an interface to set the default input state, but we can achieve the desired effect as follows:
Default Chinese:
mEditText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
Default English:
mEditText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI);
Turn IME on and off
Manually controlling the switching state of input methods can also improve the user experience, such as:
-
Some search box will have a clear button, click the Clear button should pop-up input method, because the user to clear the search for the purpose of most of the time is required to enter new content;
-
When performing a search, you should hide the input method, because the input method will obscure the search results, the user experience is not very good;
-
The input method should be hidden when the alarm clock comes in or other window pops up, because the input method is also window, if not hidden, may cause the input method to obscure other windows and other user experience is not too friendly.
Open Input Method:
private void open(Context context, View editText){ InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(editText, 0);}
To turn off IME:
private void close(Context context, View editText){ InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);}
Monitor the input status of the EditText
-
Similar to Sina Weibo, in the input content will prompt also can enter how many words;
-
Some search engines, input content in real-time display search results;
-
Some input box has the input length limit, the input content exceeds the length limit to pop up the prompt message.
The above can be achieved by listening to the input state of the edittext, the specific implementation is as follows:
mInputEditTxt.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) { System.out.println("监听EditText输入内容的变化,在这里可以监听输入内容的长度。"); } @Override public void afterTextChanged(Editable s) { System.out.println("这里可以实现所输即所得,用户输入的同时可以立即在这里根据输入内容执行操作,显示搜索结果!"); } });
Monitor the Enter button in Input method
For example, the lower right corner of the Sogou input method has a carriage return button, we want the user to click on it also performs the confirmation function, can be monitored by the EditText button click event to achieve:
/** * 监听输入法按键 * * */ mInputEditTxt.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { System.out.println("手指弹起时执行确认功能"); return true; } return false; } });
Change the display of the Enter button in the Input method
If the Enter button is a search function, the return button displays "search", or "send" if the Send function is performed, and "next" if it is the next step.
Implementing this function requires calling EditText's Setimeoptions method:
/**** IME_ACTION_SEARCH 搜索* IME_ACTION_SEND 发送* IME_ACTION_NEXT 下一步* IME_ACTION_DONE 完成*/mInputEditTxt.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
Limit input Content
Sometimes we do not want users to enter some assorted content, because this requires the program for the input content to do a variety of processing, if not properly handled there will be a lot of unforeseen problems, simply in the input of the user is forbidden to enter some illegal characters, which can be achieved by the following way, Create a new class Inputtxtfilter:
public class inputtxtfilter{public static final int input_type_en = 0x01; public static final int input_type_ch = 0x02; private static final string[] SPELL = new string[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P" , "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "Z", "ā", "á", "ǎ", "à", "ō", "Ó", "Ǒ", "Ò", "ッ", "é", "canít", "è", "ī", "í", "ǐ", "ì", "ū", " Ú "," ǔ "," Ù "," disables "," Ǘ "," clean "," Ǜ "," Ü "}; private static char[] Chineseparam = new char[]{' "', ', ', '. ‘,‘? ', ' ... ', ': ', ' ~ ', ' ' ', ' # ', ', ', '% ', ' * ', ' & ', ' $ ', ' (', ' ', ' ', ' ' ', ' ' ', ' ' ', ' ', ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ‘】‘,‘}‘,‘〕‘,‘』‘,‘”‘,‘)‘,‘! ‘,‘;‘,‘—‘}; Private Inputtxtfilter () {} public static void Inputfilter (final context context, final EditText EditText, final I NT type, final int inputlimit) {inputfilter[] filters = new INPUTFILTER[1]; Filters[0] = new Inputfilter.lengthfilter (inputlimit) {public charsequence filter (charsequence source, int star T, int end, spanned dest, int DStarT, int dend) {Boolean isrightcharater = false; if (type = = input_type_en) {isrightcharater = Isletter (source.tostring ()); }else if (type = = input_type_ch) {isrightcharater = Ischineseword (source.tostring ()); } if (!isrightcharater| | dest.tostring (). Length () >=inputlimit) {return ""; } return source; } }; Edittext.setfilters (filters); }/** * Detects if String is all Chinese * */public static Boolean Ischineseword (string name) {Boolean res=true ; char[] ctemp = Name.tochararray (); for (int i = 0; i < name.length (); i++) {if (!ischinese (ctemp[i])) {res=false; Break }} return res; }/** * is the English alphabet * * */public static Boolean Isletter (String inputstr) {char[] Inputarray = Inputstr.tochararray (); list<string> spelllist = arrays.aslist (SPELL); for (char Input:inputarray) {if (!spelllist.contains (input + "")) {return false; }} return true; }/** * Determines input kanji * @param c */public static Boolean Ischinese (char c) {for (char Param:chinese Param) {if (Param = = c) {return false; }} Character.unicodeblock UB = Character.UnicodeBlock.of (c); if (UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS | | ub = = Character.UnicodeBlock.CJK_COMPATIBILITY_IDEO GRAPHS | | UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A | | UB = = Character.UnicodeBlock.GENERAL_PUNCTUATION | | UB = = Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION | | UB = = Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {return true; } rEturn false; }}
When initializing EditText, call Inputtxtfilter's Inputfilter method, pass in input length limit, type limit of input content and so on, eg:
InputTxtFilter.inputFilter(this, mInputEditTxt, InputTxtFilter.INPUT_TYPE_EN, 5);
Copy and paste function of shielded EditText
In the lower version of the Android SDK, if the input length of the EditText is limited, press and hold EditText and drag the selected content into the EditText input box, if the length exceeds the EditText input length limit, the program will crash directly In the high version of the Android SDK this problem has been changed, if the situation above will be directly empty the input box, in order to avoid this annoying problem, we can block edittext copy and paste function, only need to shield the EditText long-press response:
/** * 屏蔽复制、粘贴功能 * * */ mInputEditTxt.setCustomSelectionActionModeCallback(new ActionMode.Callback() { public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { return false; } public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { return false; } public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { return false; } @Override public void onDestroyActionMode(ActionMode mode) { } }); mInputEditTxt.setLongClickable(false);
The use of Android EditText and notable places