标签: android-alertdialog

Android错误:无法添加窗口 - 令牌null不适用于应用程序

HY.我试图创建一个警告对话框,但是当我运行我的应用程序时,会抛出异常

09-26 12:43:21.949: E/AndroidRuntime(14618): FATAL EXCEPTION: main
09-26 12:43:21.949: E/AndroidRuntime(14618): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:687)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.app.Dialog.show(Dialog.java:278)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at com.example.stampp.UI.Rewards.DeleteRewardsFragment$1.onItemClick(DeleteRewardsFragment.java:80)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AbsListView.performItemClick(AbsListView.java:1394)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3024)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3846)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.View.dispatchTouchEvent(View.java:5629)
09-26 12:43:21.949: …
Run Code Online (Sandbox Code Playgroud)

android android-alertdialog android-dialog

26
推荐指数
3
解决办法
8万
查看次数

Android AlertDialog,每次请求都会动态更改文本

我想显示一个AlertDialog,其中一个选项可能会在每个请求中更改.因此,例如,有时我想显示"添加到联系人"选项,而另一次应该"从联系人中删除".

我的代码第一次工作,但Android似乎缓存AlertDialog,以便下次不执行onCreateDialog.因此,该选项不再改变.我可以阻止此缓存,还是只是另一种更改选项的方法?

我正在使用SDK 1.5但使用1.1.

@Override
protected Dialog onCreateDialog(final int id) {
    ...
    String add_remove_contact = res.getString(R.string.profile_add_to_contacts);
    if (user.getContacts().contains(profileID)) {
        add_remove_contact = res.getString(R.string.profile_remove_from_contacts);
        // TODO: this string is not changed when contact status changes 
    }
    final CharSequence[] items = {res.getString(R.string.view_profile),
                                  res.getString(R.string.profile_send_message),
                                  add_remove_contact};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ...
    return builder.create();
}
Run Code Online (Sandbox Code Playgroud)

java android android-alertdialog

25
推荐指数
2
解决办法
4万
查看次数

在AlertDialog后面的模糊背景

正如我们从API 14中了解到的那样,下面的模糊已被弃用

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); 
Run Code Online (Sandbox Code Playgroud)

是否有任何替代方法可以使Dialog背后的屏幕模糊我尝试了FAST模糊

android blur android-alertdialog

25
推荐指数
2
解决办法
4万
查看次数

我在哪里可以找到默认的AlertDialog布局xml文件?

我想通过一个小修改来创建一个默认的警报对话框.我在哪里可以找到默认AlertDialog的xml布局文件?

layout android android-alertdialog

25
推荐指数
1
解决办法
1万
查看次数

如何使用Dialog Fragment?(showDialog已弃用)Android

我知道有这个文档

http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog

但作为一个新的Android/Java学习者,通过编写一个弹出2个选项(是/否)消息的简单警告对话框来理解所涉及的代码量并不容易.

这是我目前在MainActivity文件中的代码:

final private int RESET_DIALOG = 0;

    private OnClickListener resetButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog(RESET_DIALOG);

        }
    };

    protected android.app.Dialog onCreateDialog(int id) {
        switch(id) {
        case RESET_DIALOG: 
            AlertDialog.Builder builder = new Builder(this);
            return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                        }
                    })

                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface arg0, int …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-alertdialog

25
推荐指数
3
解决办法
6万
查看次数

如何将图像放入AlertDialog?Android的

我不知道如何将图像放入AlertDialog.

我有这个代码,但我认为这是不可能的.

AlertDialog.Builder alert = new AlertDialog.Builder(MessageDemo.this);    
ImageView imageView = (ImageView) findViewById(R.id.imageView1);    
imageView.setImageResource(R.drawable.cw);             
alert.setView(imageView);    
alert.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dlg, int sumthin) {

    }
});    
alert.show();
Run Code Online (Sandbox Code Playgroud)

android android-alertdialog

24
推荐指数
3
解决办法
8万
查看次数

将AlertDialog按钮对齐到中心

我使用此代码进行Android(Java)编程:

public static MessageBoxResult showOk(
        Context context, String title, String message, String okMessage)
{
    okDialogResult = MessageBoxResult.Closed;

    // make a handler that throws a runtime exception when a message is received
    final Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message mesg)
        {
            throw new RuntimeException();
        }
    };

    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle(title);
    alert.setMessage(message);

    alert.setPositiveButton(okMessage, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            okDialogResult = MessageBoxResult.Positive;
            handler.sendMessage(handler.obtainMessage());
        }
    });

    AlertDialog dialog = alert.show();


    // align button …
Run Code Online (Sandbox Code Playgroud)

android button alignment android-alertdialog

24
推荐指数
5
解决办法
3万
查看次数

DialogFragment:将AlertDialog与自定义布局一起使用

我正在用Fragments API支持重写我的应用程序.在原始应用程序中,我有一个AlertDialog这样的:

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.button_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(view);
    ListView mListView = (ListView) view.findViewById(R.id.mblist);
    builder.setTitle(getString(R.string.sysinfo)).setNeutralButton(
            getString(R.string.okay), null);
    Cursor dcursor = propManager.propCursor();
    if (dcursor.moveToFirst()) {
        startManagingCursor(dcursor);
        String[] from = { Constants.NAME_DET,
                Constants.VALUE_DET };
        int[] to = { R.id.mbname, R.id.mbvalue };
        SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this,
                R.layout.info_list_row, dcursor, from, to);
        mListView.setAdapter(dadapter);
    }
    final Dialog dialog = builder.create();
    dialog.show();
Run Code Online (Sandbox Code Playgroud)

如何通过DialogFragment显示相同的对话框?我已阅读文档,但无法弄清楚如何继续这样做.

android android-fragments android-alertdialog

23
推荐指数
1
解决办法
3万
查看次数

AlertDialog主题:如何更改项目文本颜色?

当我尝试应用标准主题时 AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle("Change");

String[] info= this.getResources().getStringArray(R.array.info);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(info);

builder.setSingleChoiceItems(arrayAdapter, ....
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

注意事项是我没有问题,builder.setItems(...)因为它的文本颜色是Black在应用主题时builder.setSingleChoiceItems(...)具有白色文本颜色.

任何快速修复?或者基于什么方式创建自定义主题AlertDialog.THEME_DEVICE_DEFAULT_LIGHT

我的自定义样式无法按预期工作:

<style name="AlertDialogCustomTheme" android:parent="android:Theme.Dialog">
    <item name="android:textColor">#7ABDFF</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>


    <!--THE FOLLOWING ITEMS HAVE NOT EFFECT ... !! -->

    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:textColorAlertDialogListItem">#A844BD</item>
    <item name="android:itemBackground">#7ABDFF</item>
</style>
Run Code Online (Sandbox Code Playgroud)

更新

@lopez答案是一个完整的解决方案,但我找到了我的问题的单行修复,一个自定义主题应用于清单中的活动:

<style name="MyTheme">
    <item name="android:textColorAlertDialogListItem">@android:color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud)

android themes android-alertdialog

23
推荐指数
2
解决办法
2万
查看次数

如何在AlertDialog的布局中添加自定义按钮?

我有AlertDialog有正面和负面按钮.在AlertDialog布局中,我有EditText和两个按钮(btnAdd1,btnAdd2).我希望当用户点击Button btnAdd1或btnAdd2时,将相同的文本添加到AlertDialog中的EditText(但没有关闭AlertDialog).这可能是在AlertDialog中做的还是我只能使用Dialog?

这是AlertDialog的布局(R.layout.prompt):

<LinearLayout>
<EditText
    android:id="@+id/userInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" >

    <requestFocus />
</EditText>

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

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

  </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是源代码:

    LayoutInflater layoutInflater = LayoutInflater.from(this);
        View promptView = layoutInflater.inflate(R.layout.prompt, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(promptView);
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                              //...

                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    AlertDialog alertD = alertDialogBuilder.create();
    alertD.show();
Run Code Online (Sandbox Code Playgroud)

我想从布局中访问btnAdd1和btnAdd2.将OnClickListener()设置为这两个按钮.

layout android dialog button android-alertdialog

23
推荐指数
1
解决办法
3万
查看次数