这是我在Stackoverflow上的第一篇文章,到目前为止,在这里找到很多信息非常棒,谢谢你们!
我试图找到一个包含可选项的列表。当您按下该项目时,它可能会提示一个带有3个radiobuttons的对话框。我想从选定的单选按钮中获取ID,但现在它总是返回相同的值。因为我选择1(克莱因)默认。按下一个不同的按钮并获得该值不会有帮助。
我期待你可以提供给我的提示/技巧/帮助(我最近开始使用android编程,所以纠正我错误的地方!)
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
if (groupPosition == 4) {
LayoutInflater li = LayoutInflater.from(context);
final View myView = li.inflate(R.layout.dialog, null);
final AlertDialog.Builder Builder = new AlertDialog.Builder(context);
// set layout
Builder.setView(dialog);
// set title
Builder.setTitle("Maak uw keuze");
// set dialog message
Builder
.setCancelable(true)
.setPositiveButton("Bestellen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
RadioGroup radioGroup1 = (RadioGroup)myView.findViewById(R.id.RadioGroup);
String radiovalue = ((RadioButton)myView.findViewById(radioGroup1.getCheckedRadioButtonId())).getText().toString();
// int selectId =radioGroup1.getCheckedRadioButtonId();
dialog.cancel();
Toast.makeText(MainActivity.this, radiovalue,
Toast.LENGTH_SHORT).show();
}
});
// create alert dialog
AlertDialog alertDialog = Builder.create();
// show it
alertDialog.show();
}
my xml(dialog.xml):
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup"
android:orientation="horizontal"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checkedButton="@+id/radioButton1"
>
<RadioButton
android:text="Klein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton1"
android:layout_alignBaseline="@+id/radioButton"
android:layout_alignBottom="@+id/radioButton"
android:layout_toRightOf="@+id/radioButton"
android:layout_toEndOf="@+id/radioButton"
/>
<RadioButton
android:text="Middel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton2"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RadioButton
android:text="Groot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton3"
android:layout_alignBaseline="@+id/radioButton2"
android:layout_alignBottom="@+id/radioButton2"
android:layout_toRightOf="@+id/radioButton2"
android:layout_toEndOf="@+id/radioButton2"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp" />
</RadioGroup>
答案 0 :(得分:0)
更新代码:
.setPositiveButton("Bestellen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//keep this variables global outside onCreate just like ExpandableListView expListView;
RadioGroup radioGroup1; //this
RadioButton radioButton; // and this too keep global
radioGroup1 = (RadioGroup)myView.findViewById(R.id.RadioGroup);
int something = radioGroup1.getCheckedRadioButtonId();
radioButton = (RadioButton)myView.findViewById(something);
String radioValue = radioButton.getText().toString();
dialog.cancel();
Toast.makeText(MainActivity.this, radioValue,
Toast.LENGTH_SHORT).show();
}
});
从XML中删除属性:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup"
android:orientation="horizontal"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checkedButton="@+id/radioButton1" //removing this helped me so try to remove and apply the java code.
>
答案 1 :(得分:0)
我明白了!
我有:
LayoutInflater li = LayoutInflater.from(context); final查看myView = li.inflate(R.layout.dialog,null);
final AlertDialog.Builder Builder = new AlertDialog.Builder(context);
// set layout
Builder.setView(dialog);
这个Build.setView应该引用我的inflater ...而不是XML本身....
所以Builder.setView(myView);解决了这个问题!