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

Android 专栏收录该内容
4 篇文章 0 订阅

**

日期选择器,单选框,多选框(自定义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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button_date_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myClick"
        android:text="设置日期对话框" />

    <Button
        android:id="@+id/button_radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myClick"
        android:text="单选列表对话框" />

    <Button
        android:id="@+id/button_check"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myClick"
        android:text="多选列表对话框" />
</LinearLayout>

activity_date_and_time.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip">

        <DatePicker
            android:id="@+id/date_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dip"
            android:calendarViewShown="false"
            android:clickable="false"
            android:datePickerMode="spinner"
            android:forceDarkAllowed="false"

            android:startYear="1990"></DatePicker>

        <Button
            android:id="@+id/positive_btn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Done"/>
</LinearLayout>

check_title.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip">

        <DatePicker
            android:id="@+id/date_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dip"
            android:calendarViewShown="false"
            android:clickable="false"
            android:datePickerMode="spinner"
            android:forceDarkAllowed="false"

            android:startYear="1990"></DatePicker>

        <Button
            android:id="@+id/positive_btn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Done"/>
</LinearLayout>
  • 1
    点赞
  • 1
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值