我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:

当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。

继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。


当前回答

我也有同样的问题。editText可视性从GONE变为VISIBLE后,我必须立即设置焦点并显示软键盘。我使用以下代码实现了这一点:

new Handler().postDelayed(new Runnable() {
            
    public void run() {
//        ((EditText) findViewById(R.id.et_find)).requestFocus();
//              
        EditText yourEditText= (EditText) findViewById(R.id.et_find);
//        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

        yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0));
        yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0));                          
    }
}, 200);

它为我工作100ms的延迟,但失败没有任何延迟或只有1ms的延迟。

注释部分代码显示了另一种方法,它只适用于某些设备。我在OS版本2.2(模拟器)、2.2.1(真实设备)和1.6(模拟器)上进行了测试。

这种方法使我免去了很多痛苦。

其他回答

我用定时器。键盘可以显示编辑文本聚焦。

    edittext = (EditText) findViewById(R.id.edittext );
    edittext.requestFocus();
    edittext.setFocusableInTouchMode(true);
    if (edittext.requestFocus()) {
        final Thread timer = new Thread() {
            public void run() {
                try{
                    sleep(500);
                    InputMethodManager imm =(InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edittext, SHOW_IMPLICIT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        timer.start();

要使键盘出现,请使用

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

这个方法比直接调用InputMethodManager更可靠。

要关闭它,使用

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

我使用这个扩展函数来显示Kotlin的键盘。

fun EditText.requestFocusWithKeyboard() {
    post {
        dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0))
        dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0))
        setSelection(length())
    }
}

你可以把它叫做:

editText.requestFocusWithKeyboard()

除了非常方便之外:

您不必担心在所有可能的情况下关闭它,例如当您使用InputMethodManager.SHOW_FORCED导航返回时。 除了请求焦点,它还设置选择(光标到文本末尾)。

我发现了一个奇怪的行为,因为在我的一个应用程序中,软键盘在进入活动时自动显示(onCreate中有editText.requestFocus())。

进一步挖掘,我发现这是因为布局周围有一个ScrollView。如果我删除ScrollView,行为如原始问题声明中所述:只有在单击已经聚焦的editText时,软键盘才会显示出来。

如果这对你不起作用,试着加入一个ScrollView——无论如何这是无害的。

kotlin扩展使用下面。

fun EditText.toggle() {
   requestFocus()
   val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
   imm.showSoftInput(this, 0)
}

通过以下方式访问:

editText.toggle()