编辑:我的代码没有任何问题,真正的问题是我的图片内容,当我减少我的图片内容问题已经解决了
我希望在RadioGroup中有一个带有3个RadioButtons的警报对话框,每个RadioButtons都有一个图像而不是简单的单选按钮视图,并且根据此选择在我的mainActivity中进行更改。一切正常,除了我不能将selected_user返回到mainActivity。我搜索了很多,但我找不到解决方案。我是Android新手,请帮助我。
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" />
答案 0 :(得分:0)
如果我理解了您的问题和代码,那么您将无法返回或设置&#34; selected_user&#34;主要活动的变量,如果是,则在主要活动上添加一个类变量&amp;创建它的setter方法并设置你的&#34; selected_user&#34;变量值,请检查:
class MainActivity extends AppCompatActivity{
String mySelectedUserID = "";
public void setMySelectedUserID(String mySelectedUserID) {
this.mySelectedUserID = mySelectedUserID;
}
}
myAlert.class:
public class myAlert extends Dialog implements View.OnClickListener {
MainActivity context;
String value;
RadioButton rb1, rb2, rb3;
RadioGroup rg;
public myAlert(@NonNull MainActivity 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();
context.setMySelectedUserID(selected_user);
dismiss();
}
}