关于EditText的一些用法 还有 InputFilter

仿造一个程序很痛苦.....没有思路只能上网查查,网上又没有的时候,那可就蛋疼得很,只能自己啃文档.

不过啃文档真的是很有帮助,能得到一些 direct instruction and solution

仿造的过程中学习到了一些关于EditText的用法,记下来,好记性不如烂笔头啊


1.怎样自动弹出输入法

在获取焦点的情况下调用如下方法

 imm.showSoftInput(mEVWords, InputMethodManager.SHOW_FORCED);

imm是InputMethodManager,

imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);

这样输入法就会被强制弹出


2.怎么关闭输入法?

imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);

以下是该方法的解释,但是第二个参数为什么是0,没有给出解释

public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)

Synonym for hideSoftInputFromWindow(IBinder, int, ResultReceiver) without a result: request to hide the soft input window from the context of the window that is currently accepting input.
Parameters
windowToken:    The token of the window that is making the request, as returned by View.getWindowToken().
flags:      Provides additional operating flags. Currently may be 0 or have the HIDE_IMPLICIT_ONLY bit set. 


3.改变输入法在此EditText中右下方按键的内容

mEditText.setImeOptions(EditorInfo.IME_ACTION_DONE); //设置右下方按键为完成

这个是我当时需要的设置,还有更多其他设置

int     IME_ACTION_DONE     Bits of IME_MASK_ACTION: the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed.
int     IME_ACTION_GO           Bits of IME_MASK_ACTION: the action key performs a "go" operation to take the user to the target of the text they typed.
int     IME_ACTION_NEXT     Bits of IME_MASK_ACTION: the action key performs a "next" operation, taking the user to the next field that will accept text.
int     IME_ACTION_NONE     Bits of IME_MASK_ACTION: there is no available action.
int     IME_ACTION_PREVIOUS     Bits of IME_MASK_ACTION: like IME_ACTION_NEXT, but for moving to the previous field.
int     IME_ACTION_SEARCH   Bits of IME_MASK_ACTION: the action key performs a "search" operation, taking the user to the results of searching for the text they have typed (in whatever context is appropriate).
int     IME_ACTION_SEND     Bits of IME_MASK_ACTION: the action key performs a "send" operation, delivering the text to its target.
int     IME_ACTION_UNSPECIFIED  Bits of IME_MASK_ACTION: no specific action has been associated with this editor, let the editor come up with its own if it can.
int     IME_FLAG_FORCE_ASCII    Flag of imeOptions: used to request an IME that is capable of inputting ASCII characters.
int     IME_FLAG_NAVIGATE_NEXT  Flag of imeOptions: used to specify that there is something interesting that a forward navigation can focus on.
int     IME_FLAG_NAVIGATE_PREVIOUS  Flag of imeOptions: like IME_FLAG_NAVIGATE_NEXT, but specifies there is something interesting that a backward navigation can focus on.
int     IME_FLAG_NO_ACCESSORY_ACTION    Flag of imeOptions: used in conjunction with one of the actions masked by IME_MASK_ACTION, this indicates that the action should not be available as an accessory button on the right of the extracted text when the input method is full-screen.
int     IME_FLAG_NO_ENTER_ACTION    Flag of imeOptions: used in conjunction with one of the actions masked by IME_MASK_ACTION.
int     IME_FLAG_NO_EXTRACT_UI  Flag of imeOptions: used to specify that the IME does not need to show its extracted text UI.
int     IME_FLAG_NO_FULLSCREEN  Flag of imeOptions: used to request that the IME never go into fullscreen mode.
int     IME_MASK_ACTION     Set of bits in imeOptions that provide alternative actions associated with the "enter" key.
int     IME_NULL    Generic unspecified type for imeOptions. 

这些都是官方的文档解释

当然与之对应,在按了这些被改变的按键后会回调一个函数:

mEditText.setOnEditorActionListener(this);   //设置一个监听器来自定义这个函数

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    switch (actionId) {     //actionId 就是对应上面的一系列flag
        case EditorInfo.IME_ACTION_DONE:        //比如在这是 IME_ACTION_DONE
            checkListener.onCheck(isInputCorrect());
            return true;    //消费了事件则返回true
    }
    return false;   
}

以下是官方解释
/**
* Called when an action is being performed.
*
* @param v The view that was clicked.
* @param actionId Identifier of the action.  This will be either the
* identifier you supplied, or {@link EditorInfo#IME_NULL
* EditorInfo.IME_NULL} if being called due to the enter key
* being pressed.
* @param event If triggered by an enter key, this is the event;
* otherwise, this is null.
* @return Return true if you have consumed the action, else false.
*/

InputFilter

4.输入文字限制
我仿造的这个软件有个需求,就是只能输入英文,网上查了一番,讲得都不清楚,于是看了文档,自己写了个实现
来举个例子
InputFilter filter = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        for (int i = start; i < end; i++) {
            if (!Character.isLetter(source.charAt(i))) {
                return "";
            }
        }
        return null;
    }
};

mEditText.setFilters(new InputFilter[]{filter}); //这个显然是可以设置多个filter

下面是这个InputFilter接口重写的方法的一部分解释

Return the CharSequence that you would like to have placed there instead,
including an empty string if appropriate
or null to accept the original replacement.

这样意思就很明显了;

==在不考虑输入的source是spanned或者spannable的实例的情况下==
==如果输入的内容 (形参source 就是输入的内容) 需要改变,则返回你需要改变的字符串,可以是"" 一个空字符串==

==如果你不需要改变这个输入的内容,则返回null就行==

这算是最基础的用法了吧

思路就是 遍历整个输入的source的每一个字符,如果发现不是字母,就替换成""

当然你还可以在方法里用Matcher和Pattern来实现其他更多的需求,不过你需要对正则表达式掌握得比较熟练


如果有需求不希望输入英文的时候输入法进行联想,可以设置EditText的属性

android:inputType="textVisiblePassword"

这样输入的文字不会换行,且输入法不会进行联想

我仿造的程序中就是这么用的


5.TextInputLayout

这是一个专门用来防止EditText的布局
直接在xml文件中使用,为了防止与EditText的hint冲突建议配合TextInputEditText使用

    
    

上面两个hint,只设置一个就够了,效果是一样的

  • 设置hint之后是这样显示的:
关于EditText的一些用法 还有 InputFilter_第1张图片
hint

当没有焦点的时候,hint是处于EditText的输入框中,

当获取焦点,处于输入状态时候,hint会平滑的移动到EditText的输入框左上方

  • 当设置了错误信息就变成这样了:
textInputLayout.setErrorEnabled(true);
textInputLayout.setError("超过10个字符");
关于EditText的一些用法 还有 InputFilter_第2张图片
error

错误信息出现在EditText的左下方,

并且占据了一定的空间,所以在布局的时候要预留好这部分空间,不然整个布局跟着输入变化很难看的

你可能感兴趣的:(关于EditText的一些用法 还有 InputFilter)