How to show an text editing box “over” the screen?

跟風遠走 提交于 2019-12-02 20:46:59
Reto Meier

The easiest solution to your problem is to display your EditText within a separate dialog themed Activity that you launch from within your main (portrait-fixed) Activity.

The EditText Activity shouldn't have its orientation fixed, so it will rotate as you'd expect when you slide out the keyboard.

Creating the Text Entry Activity

Create a new Activity the contains only the EditText View and anything else you want to include (probably OK / Cancel buttons and maybe a label?). Within the manifest set its theme to Theme.Dialog.

<activity android:name="TextEntryActivity" 
          android:label="My Activity" 
          android:theme="@android:style/Theme.Dialog"/>

Fogging or Blurring the Activities behind a dialog is done by modifying the Window properties of the foreground Activity (your text entry dialog). Within it's onCreate method use getWindow().setFlags to apply blurring to any background Activities.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,  
                     WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

Launching and Reading Entered Values from the Text Entry Activity

Use startActivityForResult to launch the text entry Activity. Within that Activity call setResult to return the text string entered within the returned intent using the techniques described in this post.

Override the onActivityResult method to listen for the result from the sub Activity.

Triggering Launch on Keyboard Exposed

You can launch the text entry Activity whenever you want, but if you want to always display it when the keyboard is exposed you can capture this event explicitely.

Start by adding the android:configChanges attribute to the portrait Activity's manifest entry. It should be registered to listen for keyboardHidden.

android:configChanges="keyboardHidden"

Within that Activity, override onConfigurationChanged to launch the text entry Activity.

@Override
public void onConfigurationChanged(Configuration newConfig) {  
  Intent i = new Intent(this,TextEntryActivity.class);    
  startActivityForResult(i, STATIC_INTEGER_VALUE);
}

You may want to check to confirm the keyboard is being exposed (rather than hidden) using the newConfig variable before launching the text entry Activity.

You may also want to use the same technique to automatically return from the text entry activity when the keyboard is hidden.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!