警报对话框显示从单选按钮选择的结果,使用户再次检查



我是Android Studio的初学者,我不知道如何解决我的问题。有一个片段,我希望用户在无线电组中选择他们喜欢的内容,该组具有自己的三个单选按钮。在他们将结果发送到另一个片段之前,我需要弹出警报对话框,让用户再次检查他们的选择。有什么方法可以收集所有按钮结果并显示给用户?

public class MainActivity extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.radio_button, container, false);
RadioGroup rgdSize = view.findViewById(R.id.rdgSize);
rgdSize.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case (R.id.rdbSmall):
break;
case (R.id.rdbMedium):
break;
case (R.id.rdbLarge):
break;
}

}
});
RadioGroup rgdRank = view.findViewById(R.id.rdgRank);
rgdRank.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case (R.id.rdbFirst):
break;
case (R.id.rdbSecond):
break;
case (R.id.rdbThird):
break;
}

}
});

Button btnSend;
btnSend = view.findViewById(R.id.btnSend);
btnSend.setOnClickListener(btnSendListener);
return view;
}
private Button.OnClickListener btnSendListener = new Button.OnClickListener(){

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnSend:
new AlertDialog.Builder(getActivity())
.setTitle("Please make sure you choose correct")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.show();
}
}
};
}

这是我的 XML

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rdgSize">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Size"
android:textSize="40sp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small"
android:textSize="20sp"
android:id="@+id/rdbSmall"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium"
android:textSize="20sp"
android:id="@+id/rdbMedium"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large"
android:textSize="20sp"
android:id="@+id/rdbLarge"/>

</RadioGroup>

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/rdgSize"
android:id="@+id/rdgRank">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Rank"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
android:textSize="20sp"
android:id="@+id/rdbFirst"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"
android:textSize="20sp"
android:id="@+id/rdbSecond"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third"
android:textSize="20sp"
android:id="@+id/rdbThird"/>

</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/rdgRank"
android:text="Send"
android:id="@+id/btnSend"/>

这样,可以使单选组具有通用性:

RadioGroup rgdSize,rgdRank;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.radio_button, container, false);
rgdSize = view.findViewById(R.id.rdgSize);
rgdRank = view.findViewById(R.id.rdgRank);
Button btnSend;
btnSend = view.findViewById(R.id.btnSend);
btnSend.setOnClickListener(btnSendListener);
return view;
}

将您的警报对话框代码替换为以下代码:

private Button.OnClickListener btnSendListener = new Button.OnClickListener(){

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnSend:
int selectedId = rgdSize.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
int selectedId2 = rgdRank.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton2 = (RadioButton) findViewById(selectedId2);
String result = radioButton.getText()+""+radioButton2.getText();

new AlertDialog.Builder(getActivity())
.setTitle("Please make sure you choose correct")
.setMessage(""+result)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.setNegativeButton("CANCEL", new 
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.show();
}
}
};

您可以使用rgdRank.getCheckedRadioButtonId();获取选中的单选按钮 ID。

你可以这样做:

RadioButton rb = (RadioButton)rgdRank.findViewById(rgdRank.getCheckedRadioButtonId());//get checked radiobutton
String s =  rb.getText(); //get text of this checked radiobutton

在此之后,使用类函数显示您想要String内容。

最新更新