如何更改所选的单选按钮?

时间:2014-05-31 01:14:07

标签: android dialog radio-button onclicklistener

我有一个带有3个单选按钮的自定义对话框(在RadioGroup中),我正在尝试为每个按钮设置不同的声音。我想要一个具有特定声音的默认按钮,当选择另一个并按下OK按钮时,声音将会改变。我尝试了一种带开关的方法,但应用程序崩溃了。

以下是对话框的xml:

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

    <RadioButton
        android1:id="@+id/radio1"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:textColor="#FFFFFF"
        android1:text="cow" />

    <RadioButton
        android1:id="@+id/radio2"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:textColor="#FFFFFF"
        android1:text="dog" />

    <RadioButton
        android1:id="@+id/radio3"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:textColor="#FFFFFF"
        android1:checked="true"
        android1:text="cat" />

</RadioGroup>

<Button
    android1:id="@+id/ok"
    android1:layout_width="wrap_content"
    android1:layout_height="wrap_content"
    android1:text="OK" />

这是java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frequency = (ImageView) findViewById(R.id.frequency);   

    frequency.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialogFrequency();
        }
    });
}

public void dialogFrequency (){

        dialog = new Dialog(MainActivity.this);
        dialog.setTitle("Choose Frequeny:");
        dialog.setContentView(R.layout.activity_dialog);

                    Button ok = (Button) dialog.findViewById(R.id.ok);
        // if button is clicked, close the custom dialog
        ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }


public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio1:
            if (checked)
                radio1.setChecked(true);
                radio2.setChecked(false);
                radio3.setChecked(false);
            break;

        case R.id.radio2:
            if (checked)
                radio2.setChecked(true);
                radio1.setChecked(false);
                radio3.setChecked(false);
            break;

        case R.id.radio3:
            if (checked)
                radio3.setChecked(true);
                radio1.setChecked(false);
                radio2.setChecked(false);
            break;

        default:
            return true;;

    }
}

2 个答案:

答案 0 :(得分:0)

来自android开发者文档:

  

Dialog类是对话框的基类,但您应该避免使用   直接实例化Dialog。

对于您的情况,AlertDialog将是最佳选择。开发人员API指南介绍了如何build an AlertDialog

您可以通过adding a list实现频率选择,或者如果您想坚持使用RadioButton概念,可以create a custom layout获取AlertDialog。

答案 1 :(得分:0)

而不是使用Dialog,而是使用AlertDialog

以下是使用AlertDialog显示单选按钮的简短示例:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.androidhuman.example.sampleapplication.app.MainActivity">

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="Open dialog" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends ActionBarActivity {

    Button btn;
    AlertDialog dlg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (Button) findViewById(R.id.btn);

    dlg = new AlertDialog.Builder(this)
        .setTitle("Custom dialog")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            // User pressed OK
            Toast.makeText(MainActivity.this, "Pressed OK", Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("Cancel", null) // Do nothing if pressed cancel
        .setSingleChoiceItems(new String[]{"Radio1", "Radio2", "Radio3"}, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            switch(which){
                case 0:
                    Toast.makeText(MainActivity.this, "Radio1", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(MainActivity.this, "Radio2", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(MainActivity.this, "Radio3", Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }).create();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        dlg.show();
        }
    });

    }
// Leaving out remainings...

显示以下对话框:

Screenshot_alertdialog_radio