我有导航活动由碎片组成。目前,该应用程序可以从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>
因此,在这种情况下,面包会显示“公斤”,但如果我检查“革兰氏阴性”单选按钮,面包仍然会显示“千克”。有谁能帮助解决这个问题吗谢谢
这两项碰撞代码段的应用程序,我想通一件事出来,那就是线,如“RadioGroup中radioGroup中=(RadioGroup中)findViewById(R.id.yourRadioGroup); “或”rb_1 =(RadioButton)myView.findViewById(R.id.radioGram);“当按钮被点击时崩溃应用程序 – Taha
还有什么我可以尝试? – Taha
@Taha检查编辑后的替代方式答案...也请确保您的布局ID和单选按钮ID是正确的.. – rafsanahmad007