避免软键盘隐藏

时间:2014-07-15 05:43:38

标签: android android-edittext

我正在使用EditText并具有onEditorActionListener和TextChangedListener。我想要做的是在文本被更改时搜索朋友,甚至在按下输入时搜索朋友。 但问题是,只要我在EditText中输入Text,它就会自动隐藏键盘。我想避免这种情况。我怎么能这样做?

这就是我正在做的事情

searchET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (actionId == KeyEvent.KEYCODE_ENTER)) {
                searchFriendList(searchET.getText().toString());
                return true;
            }
            return false;
        }
    });

    searchET.addTextChangedListener(new TextWatcher() {


        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) { // TODO Auto-generated method stub
            if (s.length() > 0)
                searchFriendList(searchET.getText().toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) { // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

要显示软键盘,请执行以下操作:

//Import this
import android.view.inputmethod.InputMethodManager;

//Create object
private InputMethodManager imm;

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

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);

答案 1 :(得分:2)

添加android:windowSoftInputMode =" stateAlwaysVisible"到您在AndroidManifest.xml文件中的活动:

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible" />

在我的测试应用程序中,这显示了应用程序启动时的键盘,虽然它没有固定在那里,但可以通过按后退按钮来解除。

要确保键盘始终可见,您可能必须在应用程序中创建自己的键盘。可能使用Android来源的Android键盘:https://android.googlesource.com/platform/packages/inputmethods/LatinIME

此外,目前有一个讨论,但没有完整的解决方案:http://groups.google.com/group/android-developers/browse_thread/thread/17210d784766602d

答案 2 :(得分:0)

将此属性用于EditText:

android:imeOptions="actionNone"  

它将始终保持您的原生键盘打开/可见。