2016-12-26 131 views
1

我有导航活动由碎片组成。目前,该应用程序可以从radiogroup中选取所选的ID,并通过单击按钮将其文本显示为敬酒。但应用程序只显示已检查的单选按钮(定义为xml),如果我更改所选的单选按钮,它仍将显示按xml中的定义进行检查的那个按钮。这是java类( FirstFragment.java)单选按钮的更改未注册

public class FirstFragment extends Fragment implements View.OnClickListener { 

View myView; 

private RadioGroup radioGroup; 
private RadioButton radioButton; 
Button btnDisplay; 
String text; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View myView = inflater.inflate(R.layout.first_layout, container, false); 


    btnDisplay = (Button) myView.findViewById(R.id.btnDisplay); 
    btnDisplay.setOnClickListener(this); 

    radioGroup = (RadioGroup) myView.findViewById(R.id.radio); 
    int selectedId = radioGroup.getCheckedRadioButtonId(); 
    radioButton = (RadioButton) myView.findViewById(selectedId); 

    return myView; 
} 

@Override 
public void onClick(View v) { 
    Toast.makeText(getActivity(), 
      text = radioButton.getText().toString(), Toast.LENGTH_SHORT).show(); 
    } 

} 

这是XML文件

<RadioGroup 
     android:id="@+id/radio" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingRight="89dp"> 

     <RadioButton 
      android:id="@+id/radioGram" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16dp" 
      android:text="Gram"/> 

     <RadioButton 
      android:id="@+id/radioKilogram" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Kilogram" 
      android:textSize="16sp" 
      android:checked="true"/> 

    </RadioGroup> 

因此,在这种情况下,面包会显示“公斤”,但如果我检查“革兰氏阴性”单选按钮,面包仍然会显示“千克”。有谁能帮助解决这个问题吗谢谢

回答

1

试试这个:

private RadioButton rb_1, rb_2; 


//in on createView 
    rb_1 = (RadioButton) myView.findViewById(R.id.radioGram); 
    rb_2 = (RadioButton) myView.findViewById(R.id.radioKilogram); 

    rb_1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getActivity(), 
      rb1.getText().toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     rb_2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getActivity(), 
      rb2.getText().toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 

替代办法:在onCreateView()你已经initilized MyView的

View myView = inflater.inflate(R.layout.first_layout, container, false); 
RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radio);   
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     // checkedId is the RadioButton selected 
     switch(checkedId){ 
       case R.id.radioGram: 
        // do operations specific to this selection 
        Toast.makeText(getActivity(),"Gram selected", Toast.LENGTH_SHORT).show(); 
        break; 
       case R.id.radioKilogram: 
        // do operations specific to this selection 
        Toast.makeText(getActivity(),"KiloGram selected", Toast.LENGTH_SHORT).show(); 
        break; 
      } 
    } 
}); 
+0

这两项碰撞代码段的应用程序,我想通一件事出来,那就是线,如“RadioGroup中radioGroup中=(RadioGroup中)findViewById(R.id.yourRadioGroup); “或”rb_1 =(RadioButton)myView.findViewById(R.id.radioGram);“当按钮被点击时崩溃应用程序 – Taha

+0

还有什么我可以尝试? – Taha

+0

@Taha检查编辑后的替代方式答案...也请确保您的布局ID和单选按钮ID是正确的.. – rafsanahmad007

0

要调用

int selectedId = radioGroup.getCheckedRadioButtonId(); 
    radioButton = (RadioButton) myView.findViewById(selectedId); 

内onCreateView后写哪只打一次电话。因为每次在烤面包上显示相同的值。尽量让里面的onClick值:

public class FirstFragment extends Fragment implements View.OnClickListener { 

View myView; 

private RadioGroup radioGroup; 
private RadioButton radioButton; 
Button btnDisplay; 
String text; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View myView = inflater.inflate(R.layout.first_layout, container, false); 


    btnDisplay = (Button) myView.findViewById(R.id.btnDisplay); 
    btnDisplay.setOnClickListener(this); 

    radioGroup = (RadioGroup) myView.findViewById(R.id.radio); 
    int selectedId = radioGroup.getCheckedRadioButtonId(); 
    radioButton = (RadioButton) myView.findViewById(selectedId); 

    return myView; 
} 

@Override 
public void onClick(View v) { 
    int selectedId = radioGroup.getCheckedRadioButtonId(); 
    radioButton = (RadioButton) myView.findViewById(selectedId); 
    Toast.makeText(getActivity(), 
      text = radioButton.getText().toString(), Toast.LENGTH_SHORT).show(); 
    } 

} 
+0

这不起作用,同样的问题仍然存在,还有什么我可以尝试? – Taha