我自定义的多选框可以选择,但是一点击确认(也就是文中的yes)就闪退了呢?
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class CheckDialog extends Dialog {
private TextView tv_gethobby,tv_title;
private String msg = null;
private CheckBox cb_pro,cb_music,cb_cook,cb_run;
private SetCheckbox scb;
private Button yes;
private List<String> lists = new ArrayList<String>();
public CheckDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
setContentView(R.layout.checkbox_dialog);
cb_pro = findViewById(R.id.cb_pro);
cb_music = findViewById(R.id.cb_music);
cb_cook = findViewById(R.id.cb_cook);
cb_run = findViewById(R.id.cb_run);
yes = findViewById(R.id.yes);
tv_gethobby = findViewById(R.id.tv_gethobby);
tv_title = findViewById(R.id.tv_title);
scb = new SetCheckbox();
cb_pro.setOnCheckedChangeListener(scb);
cb_music.setOnCheckedChangeListener(scb);
cb_cook.setOnCheckedChangeListener(scb);
cb_run.setOnCheckedChangeListener(scb);
yes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!lists.isEmpty()) {
tv_title.setText("个人爱好:");
tv_gethobby.setText(lists.toString());
lists.clear();
}
dismiss();
}
});
}
class SetCheckbox implements CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox checkBox = (CheckBox) buttonView;
switch (checkBox.getId()){
case R.id.cb_pro:
lists.add(cb_pro.getText().toString()+"\n");
break;
case R.id.cb_music:
lists.add(cb_music.getText().toString()+"\n");
break;
case R.id.cb_cook:
lists.add(cb_cook.getText().toString()+"\n");
break;
case R.id.cb_run:
lists.add(cb_run.getText().toString()+"\n");
break;
}
}
}
}1
收起
正在回答
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星