开发者

Show soft keyboard when the device is landscape mode

开发者 https://www.devze.com 2023-02-06 05:03 出处:网络
This code seems not to work in Landscape mode: EditText destinationSearch = (EditText) findViewById(R.id.dest开发者_开发技巧inationSearch);

This code seems not to work in Landscape mode:

EditText destinationSearch = (EditText) findViewById(R.id.dest开发者_开发技巧inationSearch); 

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

imm.showSoftInput(destinationSearch, InputMethodManager.SHOW_IMPLICIT);

Is there any solution to show the soft keyboard in Landscape mode ?


You need to use show forced

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


The reason is that landscape mode most often put soft keyboard in a new full screen window. As Bakih said, force will work but the full screen window have more effects and so do SHOW_FORCED.

I prefer adding

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

to my EditTextStyle so I can always catch onGlobalLayout() and so on. Now you can use SHOW_IMPLICIT. Just make sure your UI looks good in such a small area and remove autoCorrect if not needed.


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

    } 
}); 


Simpler XML changes answer 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>

So add

android:imeOptions="flagNoExtractUi

and

<requestFocus />

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号