-3
我想在RadioGroup中有一個帶3個RadioButtons的警報對話框,每個對話框都有一個圖像而不是單選按鈕的簡單視圖,並且基於此選擇進行更改我的主要活動。除非我無法將selected_user返回到mainActivity,否則一切正常。我搜查了很多,但我無法找到解決方案。我是Android新手,請幫助我。Android的 - 如何在AlertDialog中使用圖像而不是RadioButtons
mainActivity.java:
myImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myAlert alert = new myAlert(this, value);//I use this value to check the item that was chosen in mainActivity before calling AlertDialog
alert.show();
}
});
myAlert.java:
public class myAlert extends Dialog implements View.OnClickListener {
Context context;
String value;
RadioButton rb1, rb2, rb3;
RadioGroup rg;
public myAlert(@NonNull Context context, String value)
{
super(context);
this.context = context;
this.value = value;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
rb3 = (RadioButton)findViewById(R.id.rb3);
if(value.equals("car"))
rb1.setChecked(true);
else if(value.equals("bus"))
rb2.setChecked(true);
else
rb3.setChecked(true);
rb1.setOnClickListener(this);
rb2.setOnClickListener(this);
rb3.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
int selectId=rg.getCheckedRadioButtonId();
RadioButton selected = (RadioButton)findViewById(selectId);
String selected_user = selected.getText().toString();
dismiss();
}}
alert.xml:
<RadioButton
android:id="@+id/rb1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/draw"
android:button="@null"
android:drawableBottom="@drawable/ib_rb1_alert"
/>
<RadioButton
android:id="@+id/rb3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/draw"
android:button="@null"
android:drawableBottom="@drawable/ib_rb3_alert" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/draw"
android:button="@null"
android:drawableBottom="@drawable/ib_rb2_alert" />