【问题标题】:Soft Keyboard shows up on EditText focus ONLY once软键盘仅在 EditText 焦点上显示一次
【发布时间】:2011-11-09 12:00:16
【问题描述】:

感谢阅读。

我面临一个奇怪的问题:我的应用程序行为是这样的,当Activity 启动时,我在EditTextrequestFocus() 并显示软键盘。

但是,当我按下后退按钮关闭键盘并点击EditText 时,我再也不会弹出键盘了。唯一的出路是再次启动Activity

我的代码如下所示:


EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm != null) {
            imm.toggleSoftInput(0, 0);
            imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);    
        }

这是我的 XML:


<EditText android:id="@+id/editText" 
        android:layout_width="wrap_content"
        android:imeOptions="actionSearch" android:hint="Test Hint"
        android:layout_height="wrap_content" android:layout_centerHorizontal="true" 
        android:maxLength="30"> 
</EditText>

任何帮助将不胜感激!

谢谢!

【问题讨论】:

    标签: android keyboard android-edittext


    【解决方案1】:

    尝试打开并隐藏在Runnable里面,

    打开

                     ettext.requestFocus();
                     ettext.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager keyboard = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);
                            keyboard.showSoftInput(ettext, 0);
                        }
                    },200);
    

    关闭

                        ettext.requestFocus();
                        ettext.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager keyboard = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);
                            keyboard.hideSoftInputFromWindow(ettext.
                                                             getWindowToken(), 0);
                        }
                    },200);
    

    【讨论】:

    • 成功了!我只是使用了打开键盘代码。我只是按返回键来隐藏键盘。谢谢! :)
    • 适用于 SDK 17 / MIN 8 / TARGET 17!
    • 在我到达这个页面之前我已经完成了同样的解决方案 - 但我只是想解释一下为什么我们的解决方案有效
    【解决方案2】:

    您使用了错误的视图来显示输入窗口。

    EditText editText = (EditText) findViewById(R.id.editText);
    editText.requestFocus();
    InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    if(imm != null) {
        imm.showSoftInput(editText, 0); 
    }
    

    【讨论】:

    • 感谢指出,我刚刚更正了。在提出问题时这是一个错字。但原始代码有正确的看法。
    【解决方案3】:

    试试这个:

    final InputMethodManager imm = (InputMethodManager)EnterWordsActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null)
        {
            imm.toggleSoftInput(YOUE_EDTITE_TEXT.SHOW_FORCED,1);
        }
    

    【讨论】:

    • 你的意思是 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 1);正确的?如果是,那没有用:(感谢您的宝贵时间。
    • @SagarHatekar 您可能在清单中添加了一些内容。我已经尝试过很多次了。它工作正常。
    【解决方案4】:

    除非我在请求焦点之前清除了textField.clearFocus(); 的力,否则以上都不起作用,所以我在onResume 中的最终代码如下所示。

       @Override
        public void onResume() {
            super.onResume();
            Log.d(TAG, "onResume: ");
            resumed = true;
            textField.postDelayed(new Runnable() {
                @Override
                public void run() {
                    textField.clearFocus();
                    textField.requestFocus();
                    if (!editMode)
                        textField.getText().clear();
                    inputMathodType = SharedPref.read(SharedPref.KEY_INPUT_MATHOD_SHARED_PREF, -1);
                    setInputMethod();
    
                }
            }, 200);
        }
    

    【讨论】:

      【解决方案5】:

      在你的活动中使用了这个编码,它会隐藏你的键盘this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

      【讨论】:

        猜你喜欢
        • 2012-03-04
        • 1970-01-01
        • 2013-09-21
        • 1970-01-01
        • 2013-07-16
        • 2011-11-18
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多