2013-06-26 3 views
0

답변이 이미 게시되었지만 나이를 찾았으며 적절한 내용을 찾을 수 없으면 사과드립니다.라디오 버튼 케이스/스위치 명령

라디오 단추가 4 개 있으며 입력 텍스트 (편집 텍스트의 텍스트)에서 어떤 것이 선택되었는지에 따라 다른 계산을 처리하려고합니다. 또한 계산을 시작하기 위해 누를 수있는 버튼이 있습니다.

전체 프로세스가 라디오 버튼과는 별도로 작동합니다. 나는 switch/case 명령을 사용해야한다는 것을 알고 있지만 그것을 설정하는 것은 확실하지 않습니다.

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> 
지금 라디오 버튼을 도입 시작할 때까지 그것은, 잘 작동했다

그것은 추락한다. 아이디어에 대해 많은 감사드립니다.

+0

레이아웃에 라디오 그룹을 사용하십시오. 라디오 그룹에는 라디오 버튼이 포함됩니다. –

답변

3

의견이 너무 길기 때문에 다른 답변으로 게시하고 있습니다. 모든 라디오 버튼

<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" 

/> 
... 

당신이 클릭 한 후 호출되는 방법 onRadioButtonClicked를 생성 라디오 버튼을 기본

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

하기 위해 현재 선택된 라디오 버튼을 저장하고 설정 지역 변수를 만들려면

설정 android:onClick="onRadioButtonClicked" 지정된 라디오 버튼을 선택하고 selectedRadio 변수를 현재 선택된 라디오 버튼으로 설정하십시오.

는 계산

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

희망이 당신을하는 데 도움을 수행 할 onClick 방법을 조정합니다. 이 코드를 테스트하지는 않았지만 작동해야합니다.

+0

시간을내어 주셔서 감사합니다. – Minnichanic