使用键盘stateAlwaysVisible,触摸后显示AlertDialog打开选择的信,为什么? [英] Using keyboard with stateAlwaysVisible, after touch any letter the showed AlertDialog opens selected, why?

查看:353
本文介绍了使用键盘stateAlwaysVisible,触摸后显示AlertDialog打开选择的信,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的布局只有一个按钮。
10(5个键盘沾上)计数后的活动显示了 AlertDialog
在清单中的活动有的android:windowSoftInputMode =stateAlwaysVisible,始终显示键盘

问题是,触摸后键盘上的任意键只有一个按钮选择一种。在蓝色的像突出了整个按钮。

我可以用低于code上的布局来解决按键问题:

 机器人:可聚焦=真
机器人:focusableInTouchMode =真

但对于 AlertDialog ,它会打开选​​中。

还与发生在 AlertDialog ,它会打开与选定的按钮中的一个。

不过,我不希望这种行为,我怎么能避免呢?

下面是我的codeS:

清单:

 <活动
   机器人:名字=。MainActivityTest
   机器人:windowSoftInputMode =stateAlwaysVisible>
< /活性GT;

活动:

 公共类MainActivityTest延伸活动{       静态诠释计数= 0;       @覆盖
       保护无效的onCreate(捆绑savedInstanceState){
           super.onCreate(savedInstanceState);
           的setContentView(R.layout.activity_main_activity_test);
       }        @覆盖
公共布尔dispatchKeyEvent(KeyEvent的事件){    数+ = 1;
    Log.d(伯爵,伯爵+计数);
    如果(计数> = 10){
        计数= 0;        InputMethodManager inputManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(getCurrentFocus()getWindowToken(),0);        AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setTitle(标题);
        builder.setMessage(信息);
        builder.setPositiveButton(R.string.yes,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释它){
                dialog.dismiss();
            }
        });        builder.setNegativeButton(R.string.no,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释它){
                dialog.dismiss();
                完();
            }
        });        builder.setOnCancelListener(新OnCancelListener(){
            @覆盖
            公共无效onCancel(DialogInterface对话){
                dialog.dismiss();
                完();
            }
        });        AlertDialog警报= builder.create();
        alert.show();
    }    返回super.dispatchKeyEvent(事件);
}
    }

布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <按钮
        机器人:ID =@ + ID / bt_testButton
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=按钮/>< / LinearLayout中>


解决方案

这是一个查看焦点问题。您可以与下面的方法解决这个问题。

更改code:

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

要:

 最后AlertDialog警报= builder.create();
    alert.setOnShowListener(新OnShowListener(){        @覆盖
        公共无效昂秀(DialogInterface对话){
            ViewParent父= alert.getButton(
                    AlertDialog.BUTTON_POSITIVE).getParent();
            如果(父的instanceof的ViewGroup){
                VG的ViewGroup =(ViewGroup中)父母;
                vg.setFocusable(真);
                vg.setFocusableInTouchMode(真);
                vg.requestFocus();
            }
        }
    });
    alert.show();

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>

解决方案

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

这篇关于使用键盘stateAlwaysVisible,触摸后显示AlertDialog打开选择的信,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆