2016-12-15 26 views
0

我有一个选项卡式活动,在其中一个片段中,我有一个带有3个单选按钮的无线电组。我想弄清楚如何确定选择哪个单选按钮。我怀疑我需要为这个片段创建一个java文件。不过,我不确定这是否正确。我跟着一个java文件的例子。我修改它,它不工作。当我点击一个单选按钮时没有任何反应。在标签片段中使用单选按钮

fragment_calories_want.xml(省略代码的问题不相关)

<RelativeLayout 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" 
    tools:context="layout.CaloriesWant"> 

    <RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="100px" 
     android:id="@+id/radioGroup" 
     android:orientation="horizontal" 
     android:layout_marginTop="82dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"> 

     <RadioButton 
      android:text="Grams" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radioButtonGrams" 
      android:layout_weight="1" 
      tools:text="Grams" /> 

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

     <RadioButton 
      android:text="Pounds" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radioButtonPounds" /> 
    </RadioGroup> 
</RelativeLayout> 

CaloriesWantFragment.java

package com.example.crims.caloriecalculator; 
import android.app.Activity; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.Toast; 

public class CaloriesWantFragment extends Fragment { 

    private RadioButton radioButton1; 
    private RadioButton radioButton2; 
    private RadioButton radioButton3; 
    private RadioGroup radioGroupOne; 
    String buttonSelected; 
    Activity activity; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 


     final View view = inflater.inflate(R.layout.fragment_calories_want, container, false); 
     radioButton1 = (RadioButton) view.findViewById(R.id.radioButtonGrams); 
     radioButton2 = (RadioButton) view.findViewById(R.id.radioButtonOunces); 
     radioButton3 = (RadioButton) view.findViewById(R.id.radioButtonPounds); 
     radioGroupOne = (RadioGroup) view.findViewById(R.id.radioGroup); 

     radioGroupOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, int i) { 
       switch(i){ 
        case R.id.radioButtonGrams: 
         buttonSelected = "button one selected"; 

         break; 
        case R.id.radioButtonOunces: 
         buttonSelected = "button two selected"; 

         break; 
        case R.id.radioButtonPounds: 
         buttonSelected = "button three selected"; 

         break; 
        default: 

       } 
       Toast.makeText(activity, "radio button selected " + buttonSelected, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     return view; 
    } 

} 
+0

如果你写的radioGroup中一行findViewById? –

+0

你可以使用'radioGroup.getCheckedRadioButtonId();'来获得选定的单选按钮ID –

+0

@RishabhMahatha我刚刚添加它。文件已更新,并仍有相同的问题。当我点击一个按钮时,期待烤面包可以显示某些东西。 – crims

回答

2

初始化无线电集团

radioGroupOne = (RadioGroup) view.findViewById(R.id.radioGroup); 

的java文件

public class CaloriesWantFragment extends AppCompatActivity { 

    private RadioButton radioButton1; 
    private RadioButton radioButton2; 
    private RadioButton radioButton3; 
    private RadioGroup radioGroupOne; 
    String buttonSelected; 
    Activity activity; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 

     radioButton1 = (RadioButton) findViewById(R.id.radioButtonGrams); 
     radioButton2 = (RadioButton) findViewById(R.id.radioButtonOunces); 
     radioButton3 = (RadioButton) findViewById(R.id.radioButtonPounds); 
     radioGroupOne = (RadioGroup) findViewById(R.id.radioGroup); 

     radioGroupOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, int i) { 
       switch (i) { 
        case R.id.radioButtonGrams: 
         buttonSelected = "button one selected"; 

         break; 
        case R.id.radioButtonOunces: 
         buttonSelected = "button two selected"; 

         break; 
        case R.id.radioButtonPounds: 
         buttonSelected = "button three selected"; 

         break; 
        default: 

       } 
       Toast.makeText(CaloriesWantFragment.this, "radio button selected " + buttonSelected, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

xml文档

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

    <RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="100px" 
     android:id="@+id/radioGroup" 
     android:orientation="horizontal" 
     android:layout_marginTop="82dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"> 

     <RadioButton 
      android:text="Grams" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radioButtonGrams" 
      android:layout_weight="1" 
      tools:text="Grams" /> 

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

     <RadioButton 
      android:text="Pounds" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radioButtonPounds" /> 
    </RadioGroup> 
</RelativeLayout> 
+0

好吧谢谢,我做到了,没有任何反应。我期待吐司显示哪个按钮被按下。 – crims

+0

我复制并粘贴了代码。我能够得到它编译/构建。它不工作(不敬酒)。我唯一的问题是这行setContentView(R.layout.fragment_calories_want);.我改变R.test到我的xml文件是否正确? – crims

+0

给我你的完整代码 –