【问题标题】:Show soft keyboard when the device is landscape mode当设备处于横向模式时显示软键盘
【发布时间】:2011-06-13 07:14:47
【问题描述】:

此代码在横向模式下似乎不起作用:

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

标签: java android landscape android-softkeyboard


【解决方案1】:

你需要使用强制显示

InputMethodManager imm;
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
imm.showSoftInput(this.editText,InputMethodManager.SHOW_FORCED);

【讨论】:

  • 为什么需要用 HIDE_IMPLICIT_ONLY 切换软输入?
【解决方案2】:

原因是横向模式最常将软键盘置于新的全屏窗口中。正如 Bakih 所说,强制会起作用,但全屏窗口会产生更多效果,SHOW_FORCED 也是如此。

我更喜欢添加

    <item name="android:imeOptions">flagNoFullscreen</item>

到我的 EditTextStyle,这样我就可以随时了解 onGlobalLayout() 等等。现在您可以使用 SHOW_IMPLICIT。只需确保您的 UI 在如此小的区域内看起来不错,如果不需要,请删除自动更正。

【讨论】:

    【解决方案3】:
    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);
    
        } 
    }); 
    

    【讨论】:

    • 您可能会发现在您的答案中添加解释很有用,这样读者就可以了解为什么您的答案是最好的,而不仅仅是发布代码。您可以按“edit”按钮添加到您的答案中。
    【解决方案4】:

    更简单的 XML 更改答案 https://stackoverflow.com/a/13179855/1029110

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

    所以添加

    android:imeOptions="flagNoExtractUi

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 2014-05-25
      相关资源
      最近更新 更多