如何使用AlertDialog预选单选按钮?

时间:2012-04-02 00:10:20

标签: java android

有人可以帮我吗?我有一个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();

请帮助!

由于

RJUY

4 个答案:

答案 0 :(得分:2)

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>

答案 1 :(得分:1)

这应该这样做。

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

您可以在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>

第一个radiobutton,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);
    }