软键盘仅在 EditText 焦点上显示一次

发布于 2024-12-02 20:59:00 字数 971 浏览 1 评论 0原文

感谢您的阅读。

我面临一个奇怪的问题:我的应用程序行为是这样的,当 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>

任何帮助将不胜感激!

谢谢!

Thanks for reading.

I am facing a strange problem: My app behavior is such that when the Activity starts, I requestFocus() on an EditText and show the soft keyboard.

However, when I press the back button to dismiss the keyboard and tap the EditText I don't get the keyboard to pop up ever again. Only way out is to the start the Activity again.

Here's what my code looks like:


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);    
        }

And here's my 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>

Any help would be greatly appreciated!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奈何桥上唱咆哮 2024-12-09 20:59:00

尝试在 Runnable 中打开和隐藏,如

TO OPEN

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

TO CLOSE

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

Try to open and hide inside a Runnable as,

TO OPEN

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

TO CLOSE

                    ettext.requestFocus();
                    ettext.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager keyboard = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                        keyboard.hideSoftInputFromWindow(ettext.
                                                         getWindowToken(), 0);
                    }
                },200);
在风中等你 2024-12-09 20:59:00

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

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

You used the wrong view for showing the input window.

EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
    imm.showSoftInput(editText, 0); 
}
金兰素衣 2024-12-09 20:59:00

试试这个:

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

try this:

final InputMethodManager imm = (InputMethodManager)EnterWordsActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null)
    {
        imm.toggleSoftInput(YOUE_EDTITE_TEXT.SHOW_FORCED,1);
    }
狼亦尘 2024-12-09 20:59:00

在请求焦点之前清除强制 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);
    }

None of above work until i clear the forces textField.clearFocus(); before requesting focus, so my final code in onResume looks like this.

   @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);
    }
清风疏影 2024-12-09 20:59:00

在您的 Activity 中使用此编码,它将隐藏您的键盘 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Used this coding in your Activity,It will hide your keyboard this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

独夜无伴 2024-12-09 20:59:00

我连续几天都面临同样的问题。通过在代码中创建线程和延迟提供的解决方案效率不高。框架有解决方案。您可以在这里查看我的简短解决方案: https://stackoverflow.com/a/74442682/6906562

I faced the same problems for days. Solutions provided with creating threads and delays in the code are not efficient. The framework has solution. you can check my short solution here: https://stackoverflow.com/a/74442682/6906562

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文