【问题标题】:How to pre select radio button using AlertDialog?如何使用 AlertDialog 预先选择单选按钮?
【发布时间】:2012-04-15 16:54:23
【问题描述】:

有人可以帮我解决这个问题吗?我有一个 AlertDialog 框,它工作正常,但我想要的是当 alertdialog 弹出时,它会自动检查一个单选按钮。

例如,弹出警告对话框时勾选“10 分钟”。

怎么做?

下面是我的工作代码。

final CharSequence[] deleteFilesBy = {"5 minutes","10 minutes", "15 minutes"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Delete Files by?")
                    .setSingleChoiceItems(deleteFilesBy,-1, new DialogInterface.OnClickListener()
                        {   
                            @Override
                            public void onClick(DialogInterface dialog, int which) 
                            {
                                if(deleteFilesBy[which]=="5 minutes")
                                {
                                    // do something
                                }
                                else if (deleteFilesBy[which]=="10 minutes")
                                {
                                    // do something
                                }
                                else if (deleteFilesBy[which]=="15 minutes")
                                {
                                    // do something
                                }
                                dialog.dismiss();
                            }
                        }).setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                // TODO Auto-generated method stub
                                arg0.dismiss();
                            }
                        });

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

请帮忙!

谢谢

RHUY

【问题讨论】:

标签: java android


【解决方案1】:

TestRadio.java:

package com.dave.kelley;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class TestRadioActivity extends Activity {

    int timeSetting = 0;
    RadioButton radio0;
    RadioButton radio1;
    RadioButton radio2;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(listener);
        radio0 = (RadioButton) findViewById(R.id.radio0);
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio2 = (RadioButton) findViewById(R.id.radio2);
    }

    public OnClickListener listener = new OnClickListener() {
        public void onClick(View v) {
            if (timeSetting == 0) {
                radio0.setChecked(true);
                radio1.setChecked(false);radio2.setChecked(false);
            }
            if (timeSetting == 1) {
                radio1.setChecked(true);
                radio0.setChecked(false);radio2.setChecked(false);
            }
            if (timeSetting == 2) {
                radio2.setChecked(true);
                radio0.setChecked(false);radio1.setChecked(false);
            }
            timeSetting++;
            if(timeSetting == 3 || timeSetting > 3) {
                timeSetting = 0;
            }
        }
    };
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

    </RadioGroup>

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

</LinearLayout>

【讨论】:

  • 所以这意味着如果我想显示 AlertDialog 例如单击“显示对话框”按钮,我将调用这个类?像 Intent i = new Intent(TestRadioActivity.class, this); startActivity(i);
  • 不,这只是为了说明我的建议。无论您有创建并显示警报对话框的代码,您都应该有: LayoutInflater inflater = getLayoutInflater();查看 v = inflater.inflate(R.layout.MAKEANXMLFILETHATLOOKSHOWYOUWANTYOURDIALOGTOLOOK); alert.setLayout(v); alert.show();
  • 没问题。如果可行,请投票/批准答案,以便其他遇到此问题的人可以找到解决方案!
  • 当然!但我现在还不能投票,因为我还有 3 分。一旦我已经有 15 个声望点,我就会这样做。再次感谢。
【解决方案2】:

应该这样做。

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.check(R.id.radioButton1);

【讨论】:

  • 我认为这个现在可以工作了,因为我没有用于 AlertDialog 的 xml 文件。 AlertDialog 是使用上面的代码创建的。不过我不确定。谢谢
【解决方案3】:

只需将选中的项目设置为您想要的...

builder.setTitle("Delete Files by?")
                .setSingleChoiceItems(deleteFilesBy, WHICH ITEM YOU WANT , new DialogInterface.OnClickListener()

【讨论】:

    【解决方案4】:

    您可以在 XML 中执行此操作:

                    <RadioButton
                        android:id="@+id/radio1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="@string/sA" />
    
                    <RadioButton
                        android:id="@+id/radio2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/A" />
    
                    <RadioButton
                        android:id="@+id/radio3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/D" />
    
                    <RadioButton
                        android:id="@+id/radio4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sD" />
                </RadioGroup>
    

    第一个单选按钮 radio1 具有属性 android:checked="true"。这意味着当它加载时,该收音机将被默认设置,当然用户仍然可以更改它。

    编辑:

    更程序化的方法:

        int timeSetting = 1;//say 0 = 5; 1=10; 2=15 (set this elsewhere in your code based
        //on how you have predetermined the time length
        RadioButton radio0 = (RadioButton) findViewById(R.id.radio0);
        RadioButton radio1 = (RadioButton) findViewById(R.id.radio1);
        RadioButton radio2 = (RadioButton) findViewById(R.id.radio2);
    
        if (timeSetting == 0) {
            radio0.setChecked(true);
            radio1.setChecked(false);radio2.setChecked(false);
        }
        if (timeSetting == 1) {
            radio1.setChecked(true);
            radio0.setChecked(false);radio2.setChecked(false);
        }
        if (timeSetting == 2) {
            radio2.setChecked(true);
            radio0.setChecked(false);radio1.setChecked(false);
        }
    

    【讨论】:

    • 但我想要的是让它动态检查。在评论“//做某事”中,我有一个代码可以将值保存在数据库中,然后每次弹出 alertdailog 时它都会查询数据库并根据保存在数据库中的数据它将动态检查单选按钮。虽然我已经有了保存数据和检索数据库中数据的代码。我现在面临的唯一问题是如何动态检查单选按钮。感谢您的回复...
    • 谢谢你,这意味着我还需要创建一个 XML 文件吗?就像你上面的帖子一样?那么我将如何初始化 AlertDialog 呢?这意味着我需要改变我的实现?谢谢
    • 嗯,是的,XML 文件为您制作项目,您可以更改布局等。但您仍然在代码中初始化它们,如我上面所示。但你也可以使用 Janhouse 所说的 - 它可能比我展示的 if 语句更容易。
    • 无论你在哪里弹出这个对话框(例如从一个监听器),你都可以这样做: LayoutInflater inflater = getLayoutInflater();查看布局 = inflater.inflate(R.layout.XMLFILEWITHRADIOGROUP); alert.setView(布局);不过,您可能需要添加另一个参数才能使 inflater.inflate 工作。看这里:stackoverflow.com/questions/9337344/…
    • 现在如何使用 AlertDialog 框来实现?我真的不知道......
    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 2015-03-04
    • 2015-05-12
    • 2023-03-02
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    相关资源
    最近更新 更多