Android editable spinner plus virtual keyboard - i

2019-08-02 01:12发布

I have AlertDialog with an editable Spinner which allows me to edit the selected value, but there is a problem. The edit control inside my Spinner accepts the focus after tap and if the device has a physical keyboard all is fine. But if no keyboard...

The virtual keyboard never shows up with this dialog. Even if I force the keyboard to come up after dialog shows (without opening the spinner list), it comes in the background only and it is unusable.

Is it possible to have such a dialog with spinner and to write in its edit field with the standard virtual keyboard? How?

My code is something like this:

final AlertDialog.Builder alertb = new AlertDialog.Builder( this );
alertb.setPositiveButton( "OK", new DialogInterface.OnClickListener() ...
alertb.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() ...

final AlertDialog alert = alertb.create();
final Spinner inputSpinner = new Spinner(this);
ArrayList<String> names = new ArrayList<String>();
//... (filling 'names' here) ...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.editforspinner, names);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
inputSpinner.setAdapter( adapter );
inputSpinner.setSelection( 0 );
alert.setView( inputSpinner, 4, 4, 4, 4 );
alert.show();

The xml file with 'editforspinner' is:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

I force the keyboard with such code:

inputSpinner.postDelayed( new Runnable() {
    @Override
    public void run() {
        InputMethodManager imm = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE );
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
    } }, 2000 );

0条回答
登录 后发表回答