Android中的完成键盘上隐藏软键盘?

时间:2010-08-05 08:59:16

标签: android android-softkeyboard soft-keyboard

我正在努力使用软键盘上的完成按钮。我无法通过软键盘完成按键来隐藏键盘。从另一个按钮,它与

完美配合

imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);

但是onKeyListener没有按照我想要的方式运行。当我点击editText时,软键盘会显示,其内容将从字符中清除。

感谢收听!

main.xml:

<EditText 
    android:id="@+id/answer" 
    android:layout_gravity="center_horizontal" android:textSize="36px"
    android:inputType="phone"
    android:minWidth="60dp" android:maxWidth="60dp"
/>

Java文件:

private EditText editText;
//...
editText = (EditText)findViewById(R.id.answer);
editText.setOnClickListener(onKeyboard);
editText.setOnKeyListener(onSoftKeyboardDonePress);
//...

// method not working:
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener() 
{
    public boolean onKey(View v, int keyCode, KeyEvent event) 
    {
        if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
        {
            // code to hide the soft keyboard
            imm = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
        }
        return false;
    }
};

private View.OnClickListener onKeyboard=new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        editText.setText("");
    }
};

使用按钮的工作方法(在同一个java文件中):

private View.OnClickListener onDone=new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        //....
        // code to hide the soft keyboard
        imm = (InputMethodManager) getSystemService(
            Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
    }
};

编辑:当我按下键“9”键盘时,键盘会隐藏。那很奇怪。

6 个答案:

答案 0 :(得分:40)

使用android:imeOptions =&#34; actionDone&#34;,就像那样:

<EditText
    ...
    android:imeOptions="actionDone" />

答案 1 :(得分:21)

InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);

将上下文作为您的活动。

答案 2 :(得分:4)

将if语句更改为if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER),使其使用xml-attribute android:inputType="phone"

答案 3 :(得分:1)

你应该看看EditText的setOnEditorActionListener():

  

设置在对其执行操作时要调用的特殊侦听器   文本视图。按下回车键或何时调用   用户选择提供给IME的动作。

答案 4 :(得分:0)

使用以下代码android:imeOptions="actionDone"为我工作。

 <EditText
    android:id="@+id/et_switch_name"       
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Name"
    android:imeOptions="actionDone"       
    android:inputType="textPersonName" />

答案 5 :(得分:0)

可以通过以下方式隐藏软键盘

在Java类中,我们可以编写以下代码以在用户按完或输入

时隐藏键盘。
etBid.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    event != null &&
                            event.getAction() == KeyEvent.ACTION_DOWN &&
                            event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
            {
                if (event == null || !event.isShiftPressed())
                {
                    // the user is done typing.
                    InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true; // consume.
                }
            }
            return false; // pass on to other listeners.
        }
相关问题