首先单选布局有问题,其次点击上面的部分对话框消失弹出吐司,点击下方部分无反应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | private void showSingleDialog() { //**************** LayoutInflater factory= LayoutInflater.from( this ); final View view=factory.inflate(R.layout.single_dialog, null ); final String[] items = { "男" , "女" , "性别未知" , "你猜" }; AlertDialog.Builder builder = new AlertDialog.Builder( this ) .setTitle( "" ) .setSingleChoiceItems(items, 0 , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity. this , "您选择了:" +items[which],Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setView(view); builder.show(); } //布局代码 <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <LinearLayout android:layout_width= "match_parent" android:layout_height= "50dp" android:orientation= "horizontal" > <ImageView android:layout_width= "40dp" android:layout_height= "wrap_content" android:layout_marginLeft= "20dp" android:src= "@mipmap/ic_launcher" /> <TextView android:id= "@+id/text" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:paddingLeft= "30dp" android:text= "请选择一下性别吧" android:textSize= "25sp" /> </LinearLayout> <RadioGroup android:id= "@+id/rg_gender" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <RadioButton android:id= "@+id/rb_male" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "男" android:textSize= "20sp" android:layout_margin= "10dp" /> <RadioButton android:id= "@+id/rb_female" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "女" android:textSize= "20sp" android:layout_margin= "10dp" /> <RadioButton android:id= "@+id/rb_unknown" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "性别未知" android:textSize= "20sp" android:layout_margin= "10dp" /> <RadioButton android:id= "@+id/rb_guess" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "你猜" android:textSize= "20sp" android:layout_margin= "10dp" /> </RadioGroup> </LinearLayout> |
0
收起
正在回答
4回答
你把第4行代码去掉就行了。只有需要在自定义对话框布局时才需要写xml呢。你的xml也没用。这是两种方式。你同时用了两种。选择一种即可。祝:学习愉快
LexieMIZUKI
2019-04-06 10:47:14
LexieMIZUKI
2019-04-03 11:12:22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | class ButtonListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.radio: showSingleDialog(); SingleDialog singleDialog = new SingleDialog(MainActivity. this , R.style.myDialog); singleDialog.show(); if (rb_female.isChecked()) Toast.makeText(MainActivity. this , "您选择了:女性。" , Toast.LENGTH_SHORT).show(); if (rb_male.isChecked()) Toast.makeText(MainActivity. this , "您选择了:男性。" , Toast.LENGTH_SHORT).show(); if (rb_unknown.isChecked()) Toast.makeText(MainActivity. this , "您选择了:性别未知!" , Toast.LENGTH_SHORT).show(); if (rb_guess.isChecked()) Toast.makeText(MainActivity. this , "您选择了:你猜!。" , Toast.LENGTH_SHORT).show(); break ; //自定义类代码 import android.app.Dialog; import android.content.Context; import android.support.annotation.NonNull; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class SingleDialog extends Dialog{ private RadioGroup rg_gender; private RadioButton rb_male,rb_female,rb_unknown,rb_guess; public SingleDialog( @NonNull Context context, int themeResId) { super (context, themeResId); setContentView(R.layout.single_dialog); rg_gender = findViewById(R.id.rg_gender); rb_male = findViewById(R.id.rb_male); rb_female = findViewById(R.id.rb_female); rb_unknown = findViewById(R.id.rb_unknown); rb_guess = findViewById(R.id.rb_guess); rg_gender.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.rb_male: rb_male.isChecked(); break ; case R.id.rb_guess: rb_guess.isChecked(); break ; case R.id.rb_unknown: rb_unknown.isChecked(); break ; case R.id.rb_female: rb_female.isChecked(); break ; } } }); } } |
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧