English 中文(简体)
Android: 在选中前用禁用的 OK 按钮创建一个单一选择对话框
原标题:Android: Create a single choice dialog with disabled OK button until selected

我要用简单的单选项目创建对话框。 默认情况下, 没有选择任何项目。 我只需要一个 " 确定 " 和 " 取消 " 按钮。 确定 " 按钮在选中某个项目之前必须一直禁用。 是否有某种内在的方法来这样做, 还是我必须创建自己的自定义对话框? 这是我目前拥有的 :

AlertDialog.Builder builder = new AlertDialog.Builder(context);

builder.setTitle(getString(R.string.lbl_MarkReviewAs))
    .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener()
    {
      public void onClick(DialogInterface dialog, int item)
      {
        selectedReviewStatusIndex = item;
        AlertDialog alertDialog = (AlertDialog)dialog;
        alertDialog.getButton(0).setEnabled(true);
      }
    })
    .setPositiveButton(getString(R.string.lbl_ButtonOK), new DialogInterface.OnClickListener()
    {
      public void onClick(DialogInterface dialog, int whichButton)
      {
        dialog.dismiss();
      }
    })
    .setNegativeButton(getString(R.string.lbl_ButtonCancel), new DialogInterface.OnClickListener()
    {
      public void onClick(DialogInterface dialog, int whichButton)
      {
        dialog.dismiss();
      }
    });

AlertDialog dialog =  builder.create();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
dialog.show();

问题在于此对话框 。 get Button (AlertDialog. BUTTON_POSITIVE) 返回无效 。 我如何访问正键?

最佳回答

设置选定项的听众并启用“OK”按钮

问题回答

我最擅长的就是小类ListActicity。这里是example

将此活动( 以显示方式) 设置为一些对话框( 最佳选择为“ @ android: standle/ Theme. DeviceDefault. Dialog ” ) ) 。

<activity android:name="YourDialogActivity"
    android:label="@string/title"
    android:theme="@android:style/Theme.DeviceDefault.Dialog">
    <intent-filter>
          ...
    </intent-filter>
</activity>

您需要显示对话框,然后禁用按钮“阳性”。

AlertDialog dialog =  builder.create();

dialog.show();

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);




相关问题
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 ...

热门标签