【问题标题】:How to change chosen radio button?如何更改选择的单选按钮?
【发布时间】:2014-07-20 20:34:13
【问题描述】:

我有一个带有 3 个单选按钮的自定义对话框(在 RadioGroup 中),我正在尝试为每个按钮设置不同的声音。 I want to have a default button with a specific sound and when pick another one and press an OK button, the sound will change.我尝试了一种使用 switch 的方法,但应用程序崩溃了。

这是对话框的 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;;

    }
}

【问题讨论】:

    标签: android dialog radio-button onclicklistener


    【解决方案1】:

    来自 android 开发者文档:

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

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

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

    【讨论】:

    • ty 用于回答,但我已经通过使用自定义对话框从不同的地方得到了答案。
    【解决方案2】:

    不要使用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...
    

    显示以下对话框:

    【讨论】:

    • 我想使用自定义对话框,我需要影响在 mainactivity 中播放的 mp 文件,这对我没有帮助。
    • 如果您想要自定义的对话框视图,您可以使用 AlertDialog.Builder.setView() 而不是使用辅助方法。
    猜你喜欢
    • 2021-04-09
    • 2017-12-07
    • 1970-01-01
    • 2011-02-15
    • 2011-05-22
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多