【问题标题】:Radio buttons case/switch command单选按钮大小写/切换命令
【发布时间】:2013-06-23 08:58:51
【问题描述】:

如果已经发布了此问题的答案,我深表歉意,但我已经寻找了很长时间,但找不到合适的答案。

我有四个单选按钮,并希望根据从输入数据(从编辑文本)中选择的一个来处理不同的计算。我还有一个按钮可以开始计算。

除了单选按钮之外,整个过程都有效。我知道我需要使用 switch/case 命令,但我不确定如何设置它。

我在主要活动 java 类中尝试了以下内容:

package com.minichanic.idealgas;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class MainActivity extends Activity implements android.view.View.OnClickListener{
    Button add, subtract, multiply, divide, mod;
    EditText pressin, volin, molin, tempin;
    RadioButton radio0, radio1, radio2, radio3;
    RadioGroup RadioGroup1;
    private int variable;

    /** Called when the activity is first created  */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RadioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);

        // Reference TextViews and Buttons 

        pressin = (EditText) findViewById(R.id.editText1);
        volin = (EditText) findViewById(R.id.editText2);
        molin = (EditText) findViewById(R.id.editText3);
        tempin = (EditText) findViewById(R.id.editText4);
        add = (Button) findViewById(R.id.button1);


        // Set listeners for when buttons are pressed 
        add.setOnClickListener(this);


    }

    /**
     * Switch statement to decide which button was pressed droidacid.com
     */
    public void onClick(View arg0) {
        // Get values from EditTexts
        double pressure = Double.parseDouble(pressin.getText().toString());
        double volume = Double.parseDouble(volin.getText().toString());
        double moles = Double.parseDouble(molin.getText().toString());
        double temperature = Double.parseDouble(tempin.getText().toString());


          // Perform relevant operations depending on radio button
          // Check which radio button was clicked


        switch(variable){
                 case R.id.radio0:
                pressure = ((moles * 8.31* temperature)/volume);
                break;
                 case R.id.radio1:
                        volume = ((moles * 8.31* temperature)/pressure);
                        break;
                 case R.id.radio2:
                    moles = ((pressure * volume) /  (temperature * 8.31));
                        break;
                 case R.id.radio3:
                    temperature = ((pressure * volume) /  (moles * 8.31));
                        break;



            }

        // Add result to Running total stored in output TextView 
        String tempresult = ""+temperature;
        TextView tresult = (EditText) findViewById(R.id.editText4);
        tresult.setText(tempresult);
        String pressresult = ""+pressure;
        TextView presult = (EditText) findViewById(R.id.editText1);
        presult.setText(pressresult);
        String molresult = ""+moles;
        TextView mresult = (EditText) findViewById(R.id.editText3);
        mresult.setText(molresult);
        String volresult = ""+volume;
        TextView vresult = (EditText) findViewById(R.id.editText2);
        vresult.setText(volresult);


    }

}

xml 布局包含:

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/pressure" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/vol" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mol" />

    <RadioButton
        android:id="@+id/radio3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/temp" />
</RadioGroup>

它工作正常,直到我开始介绍单选按钮,现在它崩溃了。 非常感谢您的任何想法。

【问题讨论】:

  • 在布局中使用单选组。在单选组中包括单选按钮。

标签: java android radio-button


【解决方案1】:

所有单选按钮都应在单选组内。请看这个例子。

http://www.mkyong.com/android/android-radio-buttons-example/

开关的使用很好。

【讨论】:

    【解决方案2】:

    我将其作为另一个答案发布,因为它太长了无法评论。

    为每个单选按钮设置android:onClick="onRadioButtonClicked"

    <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/pressure"
    
            android:onClick="onRadioButtonClicked"
    
     />
    ...
    

    创建存储当前选中单选按钮的局部变量并将其设置为默认单选按钮

    ...
    View selectedRadio;
    ...
    public void onCreate(Bundle savedInstanceState) {
        ...
        selectedRadio = findViewById(R.id.radio0);
        ...
    }
    ...
    

    创建方法onRadioButtonClicked,在单击指定的单选按钮并将我们的变量 selectedRadio 设置为当前选择的单选按钮后调用该方法

    public void onRadioButtonClicked(View view) {
        selectedRadio = view;
    }
    

    然后调整您的onClick 方法以执行计算

    public void onClick(View arg0) {
        ...
        switch(selectedRadio.getId()) {
            case R.id.radio0:
                // your code
                break;
            case R.id.radio1:
                // another code
                break;
            ...
        }
        ...
    }
    

    希望这对您有所帮助。未测试此代码,但应该可以工作。

    【讨论】:

    • 非常感谢您抽出宝贵的时间。
    猜你喜欢
    • 2016-09-05
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2020-05-20
    • 2013-09-10
    • 2012-03-23
    相关资源
    最近更新 更多