日期选择器,单选框,多选框(自定义title)对话框

**

日期选择器,单选框,多选框(自定义title)对话框

**

  1. 单选、多选对话框:可通过调用setSingleChoiceItems()或setMultiChoiceItems()设置对话框。
    final String items[] = {“男”, “女”, “未知性别”, “其他”};
    final AlertDialog.Builder radioBuilder = new AlertDialog.Builder(this);
    final AlertDialog.Builder checkBuilder = new AlertDialog.Builder(this);
    final boolean isRadio[] = {false,false,false,false,false};
    radioBuilder .setSingleChoiceItems(数据资源,默认选择按钮,监听事件);
    checkBuilder.setMultiChoiceItems(数据资源,boolean类型数组,监听事件)

  2. 单选对话框
    可以自己设置在xml文件中设置样式
    View vew = LayoutInflater.from(this).inflate(R.layout.layout的名字, null);
    final AlertDialog.Builder radioBuilder = new AlertDialog.Builder(this);
    radioBuilder.setView(view);//设置自定义布局
    通过获取RadioGroup控件设置监听事件。


MainActivity.java

public class MainActivity extends AppCompatActivity {


    private Calendar mCalendar;
    private SimpleDateFormat format;
    private AlertDialog dialog;
    private AlertDialog dialog1;
    private StringBuffer mResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    //点击事件
    public void myClick(View view) {
        switch (view.getId()) {
            case R.id.button_date_time:
                showDateTimeDiglog();
                break;
            case R.id.button_radio:
                showRadioDiglog();
                break;
            case R.id.button_check:
                showCheckDiglog();
                break;
        }
    }

    //日期对话框
    private void showDateTimeDiglog() {
        //实例化对象
        View view = LayoutInflater.from(this).inflate(R.layout.activity_date_and_time, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        mCalendar = Calendar.getInstance();
        format = new SimpleDateFormat("yyyy年MM月dd日");

        //初始化控件、数据
        final DatePicker datePicker = view.findViewById(R.id.date_picker);
        Button positive_btn = view.findViewById(R.id.positive_btn);

        //初始化
        datePicker.init(1990, 0, 01, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
                // 获取一个日历对象,并初始化为当前选中的时间
                mCalendar.set(i, i1, i2);
                Toast.makeText(MainActivity.this, format.format(mCalendar.getTime()), Toast.LENGTH_SHORT)
                        .show();
            }

        });

        //设置
        builder.setView(view)//给对话框设置自定义样式
                .setTitle("设置日期信息");
        //去button背景颜色
        positive_btn.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置监听事件
        positive_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, format.format(mCalendar.getTime()), Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        //显示
        dialog = builder.show();
    }

    //单选对话框
    private void showRadioDiglog() {
        //1.实例化对象
        final String items[] = {"男", "女", "未知性别", "其他"};
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final boolean isRadio[] = {false,false,false,false,false};
        //2.设置
        builder.setIcon(R.mipmap.star);//也可以传入自己设置title图标
        builder.setTitle("请选择性别");
        builder.setSingleChoiceItems(items,  5, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                switch (i) {
                    case 0:
                    case 1:
                        Toast.makeText(MainActivity.this, "你选择了:"+items[i] + "性", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(MainActivity.this, "你选择了:"+items[i], Toast.LENGTH_SHORT).show();
                }
                dialog1.dismiss();//关闭对话框
            }
        });
        //显示
        dialog1 = builder.show();
    }

    //复选框对话框
    private void showCheckDiglog() {
        //初始化数据
        final String[] items = {"唱", "跳", "Rap", "篮球", "刷b站"};
        final boolean[] choice = {false, false, false, false, false};
        mResult = new StringBuffer();
        //实例化对象
        View titleView = LayoutInflater.from(this).inflate(R.layout.check_title, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCustomTitle(titleView)//设置title样式
                .setMultiChoiceItems(items, choice, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        if (choice[i]) {
                            mResult.append(items[i] + " ");
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mResult.deleteCharAt(mResult.length() - 1);
                        Toast.makeText(MainActivity.this, mResult, Toast.LENGTH_SHORT).show();
                    }
                }).show(); //显示
    }
}

activity_main.xml





    

activity_date_and_time.xml



        

        

check_title.xml



        

        

你可能感兴趣的:(Android)