简体   繁体   English

当有两个单选组时检查单选按钮 ID

[英]Check radio button id when there're two radio groups

I am a beginner in Android programming, and trying to create a simple program that converts inches to feet, and US dollars to Japanese Yen.我是 Android 编程的初学者,并试图创建一个简单的程序,将英寸转换为英尺,将美元转换为日元。 I have the following two radio groups in my main_activity.xml file:我的 main_activity.xml 文件中有以下两个无线电组:

 <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/distanceInput"
        android:layout_below="@+id/distanceInput"
        android:layout_marginTop="10dp" >

        <RadioButton
            android:id="@+id/inch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:checked="true"
            android:onClick="handleClickOne"
            android:text="Inches" />

        <RadioButton
        android:id="@+id/foot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/currencyInput"
        android:layout_below="@+id/radioGroup1"
        android:onClick="handleClickOne"
        android:text="Feet" />

    </RadioGroup>

    <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/currencyInput"
        android:layout_below="@+id/currencyInput"
        android:layout_marginTop="10dp" >

        <RadioButton
            android:id="@+id/japan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/radioGroup2"
            android:layout_centerVertical="true"
            android:checked="true"
            android:text="JPY" 
            android:onClick="handleClickTwo"
            />

        <RadioButton
            android:id="@+id/usdollar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/euro"
            android:layout_below="@+id/euro"
            android:text="US Dollars" 
            android:onClick="handleClickTwo"
            />
    </RadioGroup>

This following code is for my handleClickOne() method that checks which radio button is selected between Inches and Feet, but the switch-case loop always falls on the default case - I wonder why:以下代码适用于我的 handleClickOne() 方法,该方法检查在英寸和英尺之间选择了哪个单选按钮,但 switch-case 循环总是落在默认情况下 - 我想知道为什么:

public void handleClickOne(View view) {
    DecimalFormat df = new DecimalFormat("#.##");


    boolean checked = ((RadioButton) view).isChecked();

    EditText distance = (EditText) findViewById(R.id.distanceInput);
    double distanceInput = parseStringToDouble(distance.getText().toString());

    switch(view.getId()) { 
    case R.id.foot:
        // convert inches to feet
        if (checked) {
            distance.setText(df.format(inchToFoot(distanceInput)));
        }
        break;

    case R.id.inch:
        //convert feet to inches
        if (checked) {
            distance.setText(df.format(footToInch(distanceInput)));
        }
        break;

    default:  
        distance.setText("000");
        break;
    }
}

My question is whether I need to select a radio button in a radio group?我的问题是我是否需要 select 单选按钮组中的单选按钮? and if so how?如果是这样怎么办?

Use利用

RadioGroup.setOnCheckedChangeListener 

rather than onclick to see which radio button is set.而不是onclick来查看设置了哪个单选按钮。 I dont think onclick works reliably for radiobutton我认为 onclick 不能可靠地用于单选按钮

I'm not sure if this is the best way to do, but the following works for now:我不确定这是否是最好的方法,但目前以下方法有效:

Because I know which click handler method works for which radio group, so in each of my click handler methods (in this case handleClickOne() ), I get the radioGroup object first:因为我知道哪个单击处理程序方法适用于哪个单选组,所以在我的每个单击处理程序方法(在本例中为handleClickOne() )中,我首先获得了 radioGroup object:

RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);

then do my switch-case in this way:然后以这种方式做我的开关盒:

switch(radioGroup1.getCheckedRadioButtonId()) { 
        case R.id.foot:
        // convert inches to feet
        if (checked) {
            distance.setText(df.format(inchToFoot(distanceInput)));
        }
        break;

    case R.id.inch:
        //convert feet to inches
        if (checked) {
            distance.setText(df.format(footToInch(distanceInput)));
        }
        break;

    default:
            distance.setText("error");
            break;
}

It works for now, but I'm still interested in learning the best practice.它现在有效,但我仍然对学习最佳实践感兴趣。 Thanks to all谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM