在按下返回键时隐藏软键盘 [英] Hide Soft keyboard on return key press

查看:287
本文介绍了在按下返回键时隐藏软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上搜索了六个其他答案,但没有找到一个可行的答案。我要做的就是在用户按下Enter键时关闭软键盘。 (等效于疯狂的iOS resignKeyboard调用。)在以下代码中,不会调用onEditorAction方法。我已经在XML文件中设置了一个EditText视图,片段中的代码如下:

  @Override 
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
fragView = inflater.inflate(R.layout.fragment_encrypt2,container,false);
textField =(EditText)fragView.findViewById(R.id.textField);

textField.setOnEditorActionListener(new TextView.OnEditorActionListener(){

@Override
public boolean onEditorAction(TextView arg0,int actionId,
KeyEvent arg2) {
//隐藏键盘并在按下输入键
//时搜索网页
if(actionId == EditorInfo.IME_ACTION_GO
|| actionId == EditorInfo。 IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_SEARCH
||((arg2.getAction()= = KeyEvent.KEYCODE_ENTER)){
InputMethodManager imm =(InputMethodManager)getActivity()。getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWin dow(textField.getWindowToken(),0);

返回true;
}
返回false;
}
});

//为该片段填充布局
return fragView; // inflater.inflate(R.layout.fragment_encrypt2,container,false);
}

以下是XML文件的片段,我在其中定义了EditText字段。我需要EditText为多行。

 < EditText 
android:id = @ + id / textField
android:layout_width = match_parent
android:layout_height = match_parent
android:hint = @ string / Encrypted_Text_Hint
android:gravity = top
android:inputType = textMultiLine
android:imeOptions = actionDone />


解决方案

如果您不希望多行,请为您编辑文本可以只为edittext指定一行,也可以将imeOptions像这样完成

 < EditText 
android :id = @ + id / edittext_done
android:layout_width = fill_parent
android:layout_height = wrap_content
android:imeOptions = actionDone
android:singleLine = true
/>

无论您是否尝试实现此目标,我都显然不知道。 p>

编辑:由于API 3由于性能不佳,不推荐使用android:singleLine,而必须使用android:maxLines。 singleLine将可用,因为即使是现在,android:maxLines属性也不支持某些效果。


I've searched half a dozen other answers on SO, but haven't found one that works. All I'm trying to do is dismiss the soft keyboard when the user presses the enter button. (The equivalent of the insanely easy iOS 'resignKeyboard' call.) In the following code, the onEditorAction method does not get called. I've set up an EditText view in my XML file and the code in my fragment is as follows:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragView = inflater.inflate(R.layout.fragment_encrypt2, container, false);
        textField = (EditText) fragView.findViewById(R.id.textField);

        textField.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView arg0, int actionId,
                                          KeyEvent arg2) {
                // hide the keyboard and search the web when the enter key
                // button is pressed
                if (actionId == EditorInfo.IME_ACTION_GO
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || actionId == EditorInfo.IME_ACTION_NEXT
                        || actionId == EditorInfo.IME_ACTION_SEND
                        || actionId == EditorInfo.IME_ACTION_SEARCH
                        || (arg2.getAction() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(textField.getWindowToken(), 0);

                    return true;
                }
                return false;
            }
        });

        // Inflate the layout for this fragment
        return fragView;//inflater.inflate(R.layout.fragment_encrypt2, container, false);
    }

The following is a snippet from the XML file where I define the EditText field. I need EditText to be multiline.

<EditText
            android:id="@+id/textField"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="@string/Encrypted_Text_Hint"
            android:gravity="top"
            android:inputType="textMultiLine"
            android:imeOptions="actionDone"/>

解决方案

If you dont want multiple line,for you edittext you can just specify single line for the edittext and also you can put imeOptions as Done like this

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:singleLine="true"
   />

I clearly dunno whether you are trying to achieve this or not.Any way take a look.

EDIT: android:singleLine is deprecated since API 3 due to bad performance, you have to use android:maxLines instead. singleLine will be available because even now some effects are not supported by android:maxLines attribute.

这篇关于在按下返回键时隐藏软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆