创建对话框,里面有2个单选按钮

时间:2014-04-16 05:28:26

标签: android

我想在对话框内单击按钮时创建一个对话框我需要2个单选按钮,这些按钮与移动默认音乐链接,其他是移动设备的外部存储器。任何人都可以帮我解决这个问题,我的代码如下?

    buttonSound = (Button) findViewById(R.id.btn_sound);
        buttonSound.setOnClickListener (new OnClickListener(){
            public void onClick(View v){
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getApplicationContext());
                Log.d("TAG","button inside mobile");
                alertDialogBuilder
                //.setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Sound",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        AlarmActivity.this.finish();
                    }
                  });
//              builder.setPositiveButton("Sound", );
                alertDialogBuilder
                //.setMessage("Click yes to exit!")
                                                                                                            .setCancelable(false)
                .setPositiveButton("SdCard",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity

                        Log.d("TAG","button inside sdCard");
                        AlarmActivity.this.finish();
                    }
                  });
                 alertDialogBuilder.show();

它显示的错误如下所示

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

3 个答案:

答案 0 :(得分:0)

试试这个..

改变这个..

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getApplicationContext());

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        AlarmActivity.this);

修改

<强> radio_btn.xml

<?xml version="1.0" encoding="UTF-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/status_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/default_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default music" />

    <RadioButton
        android:id="@+id/external_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="External card music" />

</RadioGroup>

<强> JAVA

    LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.radio_btn, (ViewGroup) getCurrentFocus());
    RadioButton default_btn = (RadioButton) dialoglayout.findViewById(R.id.default_btn);
    RadioButton external_btn = (RadioButton) dialoglayout.findViewById(R.id.external_btn);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        AlarmActivity.this);
                Log.d("TAG","button inside mobile");
                alertDialogBuilder
                //.setMessage("Click yes to exit!")
                .setCancelable(false)
                .setView(dialoglayout);
                .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        AlarmActivity.this.finish();
                    }
                  });
                 alertDialogBuilder.show();

答案 1 :(得分:0)

使用AlarmActivity.this或此getApplicationContext()的内容。

在res / layout

中的布局中创建
  1. <强> radio_dialog_layout.xml

    <RadioButton
        android:id="@+id/rd_!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A" />
    
    <RadioButton
        android:id="@+id/rd_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rd_!"
        android:text="B" />
    
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rd_2"
        android:layout_centerInParent="true"
        android:text="OK" />
    </RelativeLayout>
    
  2. 使用此代码在活动中创建对话框:

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.radio_dialog_layout);
    dialog.setTitle("This is my custom dialog box");
    dialog.setCancelable(true);
    // there are a lot of settings, for dialog, check them all out!
    // set up radiobutton
    RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_);
    RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2);
    
    // now that the dialog is set up, it's time to show it
    dialog.show();
    
  3. 您还可以参考本教程:How to add lists, Checkboxes and Radio buttons to an AlertDialog – Android

答案 2 :(得分:0)

创建一个xml文件,

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

<RadioButton
    android:id="@+id/radiobutton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radiobutton1"
    android:text="Bye" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radiobutton2"
    android:layout_centerInParent="true"
    android:text="Click" />

并在活动中创建对话框,

Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.activity_main);
    dialog.setTitle("My Dialog Box");
    dialog.setCancelable(true);

    // set up radiobutton inside dialog box
    RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.radiobutton1);
    RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.radiobutton2);

    // Show the dialog
    dialog.show();