2016-01-28 43 views
2

我有2個輻射羣。第一個有4個按鈕,第二個有3個按鈕。 我需要一個解決方案來更新TextView中的計時器,具體取決於兩個radiogroup中檢查的內容。在第一個radiogroup用戶選擇大小,第二個用戶選擇形狀。這給了12個組合。到目前爲止,我只想到如何更新1個radiogroup中的TextView字段。在下面的代碼中,我已經顯示了來自第一個radiogroup(rg1)的2個值。他們應該根據在radiogroup 2(rg2)中選擇的內容進行更改在Android中合併2個輻射羣

我找到了解決方案並更新了代碼。希望它能幫助別人。

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener { 

    TextView timer; 
    RadioGroup rg1; 
    RadioGroup rg2; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     timer = (TextView) findViewById(R.id.softTimer); 

     rg1 = (RadioGroup) findViewById(R.id.myRadioGroup1); 
     rg1.setOnCheckedChangeListener(this); 

     rg2 = (RadioGroup) findViewById(R.id.myRadioGroup2); 
     rg2.setOnCheckedChangeListener(this); 


    } 

@Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     RadioButton s1 = (RadioButton) findViewById(R.id.size_small); 
     RadioButton s2 = (RadioButton) findViewById(R.id.size_medium); 
     RadioButton s3 = (RadioButton) findViewById(R.id.size_large); 
     RadioButton s4 = (RadioButton) findViewById(R.id.size_xlarge); 
     RadioButton b1 = (RadioButton) findViewById(R.id.boil_soft); 
     RadioButton b2 = (RadioButton) findViewById(R.id.boil_medium); 
     RadioButton b3 = (RadioButton) findViewById(R.id.boil_hard); 

     if (s1.isChecked() && b1.isChecked()){ 
      tid = 204000; 
      eggTimer.setText("00:03:24"); 
     } 
     else if (s1.isChecked() && b2.isChecked()) { 
      tid = 255000; 
      eggTimer.setText("00:04:15"); 
     } 
     else if (s1.isChecked() && b3.isChecked()) { 
      tid = 341000; 
      eggTimer.setText("00:05:41"); 
     } 
     else if (s2.isChecked() && b1.isChecked()){ 
      tid = 239000; 
      eggTimer.setText("00:03:59"); 
     } 
     else if (s2.isChecked() && b2.isChecked()) { 
      tid = 279000; 
      eggTimer.setText("00:04:39"); 
     } 
     else if (s2.isChecked() && b3.isChecked()) { 
      tid = 396000; 
      eggTimer.setText("00:06:36"); 
     } 
     else if (s3.isChecked() && b1.isChecked()){ 
      tid = 270000; 
      eggTimer.setText("00:04:30"); 
     } 
     else if (s3.isChecked() && b2.isChecked()) { 
      tid = 325000; 
      eggTimer.setText("00:05:25"); 
     } 
     else if (s3.isChecked() && b3.isChecked()) { 
      tid = 485000; 
      eggTimer.setText("00:08:05"); 
     } 
     else if (s4.isChecked() && b1.isChecked()){ 
      tid = 336000; 
      eggTimer.setText("00:05:36"); 
     } 
     else if (s4.isChecked() && b2.isChecked()) { 
      tid = 400000; 
      eggTimer.setText("00:06:40"); 
     } 
     else if (s4.isChecked() && b3.isChecked()) { 
      tid = 603000; 
      eggTimer.setText("00:10:03"); 
     } 

     final CounterClass timer = new CounterClass(tid, 1000); 
     tv_start_egg1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       timer.start(); 
      } 
     }); 

     tv_stop_egg1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       timer.cancel(); 
       tid = 0; 
       eggTimer.setText("00:00:00"); 

      } 

     }); 
    } 
} 
+0

請張貼您的解答作爲答案並接受它。這樣問題將被列爲答案。謝謝。 – David

+0

因此,我應該用舊的替換代碼,發佈新的解決方案,然後將其設置爲答案?對不起,但我是這個網站的新手:-) –

+0

無需更改問題代碼。我認爲任何人都不會從錯誤的代碼中受益。重點是,如果任何人看到你的「在Android中組合2個無線電組」問題,他們應該找到答案。將其置於公認的答案中的唯一原因是其他人可以看到它已被回答。 – David

回答

0

問題已解決。我已經更新了代碼。希望它能幫助別人。

0

您可以使用此得到檢查單選按鈕ID從單選按鈕組2

int radioButtonID = rg2.getCheckedRadioButtonId(); 

現在相應的處理您的條件。

+0

如何處理交換機中的2個radiogroups ID? –

0

很容易。就像這樣:

@Override 
public void onCheckedChanged(RadioGroup group, int checkedId) { 
    switch(group.getId()) { 
     case R.id.myRadioGroup1: 
      //We are in the first radio group. 
      switch(checkedId) { 
       case R.id.size_small: 
        timer.setText("00:04:15"); 
        break; 
       case R.id.size_large: 
        timer.setText("00:01:15"); 
        break; 
      } 
      break; 
     case R.id.myRadioGroup2: 
      //We are in the second radio group. Do as you need. 
      break; 
    } 
} 

不過,我很困惑,爲什麼你不只是使用int checkedId爲第二組爲好。每個選中的單選按鈕都有一個不同的ID。

+0

我找到了解決方案。解決方案的問題在於,timer.setText不計算radiogroup 1和radiogroup 2中的值。如果我在第一個按鈕中檢查radiogroup 1,並在第一個按鈕中檢查radiogroup 2,則會獲取定時器中使用的一個值。的setText。如果我將radiogroup2更改爲按鈕2並保持按鈕1中的radiogroup檢查,我爲timer.setText獲得一個新值。我已將代碼更新爲我找到的解決方案。 –

+0

那麼,這是一個不同於問的問題:)你只是想得到第二個radiogroup點擊,這是我的回答。我很高興你解決了! – Seth