Using keyboard with stateAlwaysVisible, after touch any letter the showed AlertDialog opens selected, why?

心已入冬 提交于 2019-12-25 07:44:07

问题


I have only one button in my layout. The activity after count of 10 (five keyboard touchs) shows an AlertDialog. On Manifest the activity has the android:windowSoftInputMode="stateAlwaysVisible", to always show the keyboard.

The problem is that after touch any key on keyboard the only one button is kind selected. Like "highlighted" the entire button in blue.

I could use below code on layout to solve the button problem:

android:focusable="true"
android:focusableInTouchMode="true"

But for the AlertDialog, it will opens selected.

Also happens with the AlertDialog, it opens with one of the buttons selected.

But I don't want this behavior, how can I avoid it?

Below are my codes:

Manifest:

<activity
   android:name=".MainActivityTest"
   android:windowSoftInputMode="stateAlwaysVisible">
</activity>

Activity:

    public class MainActivityTest extends Activity {

       static int count = 0;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main_activity_test);
       }

        @Override
public boolean dispatchKeyEvent(KeyEvent event) {

    count += 1;
    Log.d("Count: ", "Count: " + count);
    if(count >= 10){
        count = 0;

        InputMethodManager inputManager = (InputMethodManager)  getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Title");
        builder.setMessage("Message");
        builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });

        builder.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                finish();
            }
        });

        AlertDialog alert = builder.create();
        alert.show();
    }

    return super.dispatchKeyEvent(event);
}
    }

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/bt_testButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

回答1:


This is a View Focus problem. You can solve it with the method below.

Change your code:

  AlertDialog alert = builder.create();
  alert.show();

To:

    final AlertDialog alert = builder.create();
    alert.setOnShowListener(new OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            ViewParent parent = alert.getButton(
                    AlertDialog.BUTTON_POSITIVE).getParent();
            if (parent instanceof ViewGroup) {
                ViewGroup vg = (ViewGroup) parent;
                vg.setFocusable(true);
                vg.setFocusableInTouchMode(true);
                vg.requestFocus();
            }
        }
    });
    alert.show();


来源:https://stackoverflow.com/questions/25071788/using-keyboard-with-statealwaysvisible-after-touch-any-letter-the-showed-alertd

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