English 中文(简体)
Android IME: showing a custom pop-up dialog (like Swype keyboard) which can enter text into the TextView
原标题:

I m wondering how I can create a custom pop-up like the one in the screenshot below (borrowed from the Swype keyboard), where I can have a couple of buttons, which each commit a string to the currently "connected" TextView (via a InputConnection).

Please note: this is an InputMethodService and not an ordinary Activity. I already tried launching a separate Activity with Theme:Dialog. However, as soon as that one opens I lose my focus with the TextView and my keyboard disappears (and with that my InputConnection is gone).

Swype

最佳回答

You can try using a PopupWindow. You ll have to do a bit of hacking to get it to do what you want and the only good documentation for it is the source.

问题回答

I was banging my head against this problem too and I finally figured it out. The above solutions are correct although as you pointed out they cannot be used from an InputMethodService because it is not an Activity. The trick is to create the PopupWindow in a subclass of KeyboardView. By using a negative Y position, the PopupWindow can appear above the keyboard like Swype.

Good luck, Barry

Correct answer:

  1. Create a PopupWindow and put your view inside it
  2. Call popupWindow.setClippingEnabled(false)
  3. Call [popupWindow.showAtLocation()](http://developer.android.com/reference/android/widget/PopupWindow.html#showAtLocation(android.view.View, int, int, int)) with a negative Y coordinate.

This will show your popup above the IME as in your screenshot.

Peace be upon those who follow the guidance,

solution :

AlertDialog dialog;

//add this to your code
dialog = builder.create();
Window window = dialog.getWindow(); 

WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;

window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//end addons

dialog.show();

===== UPDATE 30.09.2015 mInputView its the general name of your keyboard class ..see

@Override
    public View onCreateInputView() {
        mInputView =(MyKeyboardView) getLayoutInflater().inflate( R.layout.input, null);
....
}

More info : http://developer.android.com/guide/topics/text/creating-input-method.html

good luck.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签