设备横屏时显示软键盘

9

这段代码似乎无法在横屏模式下正常工作:

EditText destinationSearch = (EditText) findViewById(R.id.destinationSearch); 

destinationSearch.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(destinationSearch, InputMethodManager.SHOW_IMPLICIT);

有没有办法在横屏模式下显示软键盘?

似乎使用SHOW_FORCE标志可以正常工作:D - andreea
4个回答

12
你需要使用 "show forced"。
InputMethodManager imm;
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
imm.showSoftInput(this.editText,InputMethodManager.SHOW_FORCED);

为什么需要使用HIDE_IMPLICIT_ONLY来切换软键盘输入? - IgorGanapolsky

3
原因是横屏模式通常会将软键盘放在一个新的全屏窗口中。正如Bakih所说,强制方式可以起作用,但全屏窗口具有更多的效果,SHOW_FORCED也是如此。
我更喜欢添加:
    <item name="android:imeOptions">flagNoFullscreen</item>

我希望把这个EditText样式设置为全局布局,以便我能够始终捕捉到onGlobalLayout()等事件。现在可以使用SHOW_IMPLICIT。只需确保您的UI在如此小的区域内看起来很好,并且如果不需要,则删除autoCorrect。


0
EditText editText = (EditText)findViewById(R.id.editText);

editText.setFocusableInTouchMode(false);

final Context context = this;

editText.setOnClickListener(new OnClickListener() {

    @Override 
    public void onClick(View v) {

        v.requestFocusFromTouch();

        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);

    } 
}); 

2
你可能会发现为你的答案添加解释很有用,这样读者就可以看到为什么你的答案是最好的,而不仅仅是发布代码。你可以通过按“编辑”按钮来添加你的答案。 - Brian Tompsett - 汤莱恩

0

更简单的XML修改答案https://dev59.com/8m855IYBdhLWcg3wcz5h#13179855

 <EditText   
        android:id="@+id/txtInLandscapeVw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="flagNoExtractUi">   
        <requestFocus />
    </EditText>

所以添加

android:imeOptions="flagNoExtractUi"

<requestFocus />


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接