如果无线电组是空的

时间:2014-07-31 08:16:24

标签: android radio-button

我正在尝试一个包含6个单选按钮的广播组,我设置了我的收音机按钮但是我想设置一个对话框,如果用户没有检查任何单选按钮并单击转换按钮对话说... 我的java代码:

public class Third extends Activity    {

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

    Button d = (Button) findViewById(R.id.btn4);
    d.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                    dialog();
                }
            public void dialog(){


                final Dialog dialog = new Dialog(Third.this);
                dialog.setContentView(R.layout.dialog);
                dialog.setTitle("please enter a unit");
                dialog.show();
                 RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.rg);
                rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.rb1);
                  public void onCheckedChanged(RadioGroup arg0, int arg1) {
                       if (rb1.isChecked()){

                         dialog.dismiss();
                         Button btc = (Button)findViewById(R.id.btn5);
                         btc.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {

                            EditText ft1 = (EditText)findViewById(R.id.f3);
                            TextView txv3 = (TextView)findViewById(R.id.txv3);
                            if(ft1.getText().toString().length() == 0 ){return;}
                            int first = Integer.parseInt(ft1.getText().toString());
                            double equal = first *0.0328;   
                            DecimalFormat formatf = new DecimalFormat("#.####");
                            String x = formatf.format(equal)+"    feet or foot";
                            txv3.setText(x);}
                    });
                }
                else { 

                     Button no = (Button)findViewById(R.id.btn5);
                    no.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            dialog();
                        }

                        public void dialog(){

                            final Dialog dialog = new Dialog(Third.this);
                            dialog.setContentView(R.layout.dialog_nothing);
                            dialog.setTitle("The unit is not chosse");
                            dialog.show();
                        }
                    });

2 个答案:

答案 0 :(得分:0)

以下是检查RadioGroup中是否已检查任何单选按钮的示例。

MainActivity.java

package com.example.test;

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;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

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

        final RadioGroup radioButtonContainer = (RadioGroup) findViewById(R.id.radioButtonContainer);
        final Button go = (Button) findViewById(R.id.go);
        final Button clear = (Button) findViewById(R.id.clear);

        go.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int checkedRadioButtonId = radioButtonContainer.getCheckedRadioButtonId();

                if (checkedRadioButtonId != -1) {
                    RadioButton checkedRadioButton = (RadioButton) radioButtonContainer.findViewById(checkedRadioButtonId);
                    Toast.makeText(MainActivity.this, "The radio button with text: " + checkedRadioButton.getText().toString() + " has been checked", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "No Radio Buttons have been checked, show the dialog", Toast.LENGTH_SHORT).show();
                }
            }
        });

        clear.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                radioButtonContainer.check(-1);
            }
        });
    }
}

activity_main.xml中

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="${packageName}.${activityClass}" >

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

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

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

        <RadioButton
            android:id="@+id/radThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton 3" />
    </RadioGroup>

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

    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear Check" />

</LinearLayout>

答案 1 :(得分:0)

您的条件表达式错误,请将rb1.isChecked()更改为rg.getCheckedRadioButtonId() != -1以解决您的问题。

我没有看到任何关于&#34;显示乘法结果&#34;的代码,请确保将它们添加到正在检查的RadioButton分支中。