欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个原生Android的日期选择器,多种样式可供选择器

程序员文章站 2022-03-02 18:33:19
...

一个原生Android的日期选择器,多种样式可供选择

  1. 日期选择器
 /**
     * 日期选择
     *
     * @param activity
     * @param themeResId
     * @param tv
     * @param calendar
     */
    public static void showDatePickerDialog(Activity activity, int themeResId, final Button tv, Calendar calendar) {
        // 直接创建一个DatePickerDialog对话框实例,并将它显示出来
        DatePickerDialog dialog = new DatePickerDialog(activity, themeResId, new DatePickerDialog.OnDateSetListener() {
            // 绑定监听器(How the parent is notified that the date is set.)
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // 此处得到选择的时间,可以进行你想要的操作
                tv.setText("您选择了:" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        }
                // 设置初始日期
                , calendar.get(Calendar.YEAR)
                , calendar.get(Calendar.MONTH)
                , calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(activity.getResources().getColor(R.color.icu_blue));
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setGravity(Gravity.RIGHT);

    }

2.时间选择器

 /**
     * 时间选择
     *
     * @param activity
     * @param themeResId
     * @param tv
     * @param calendar
     */
    public static void showTimePickerDialog(Activity activity, int themeResId, final Button tv, Calendar calendar) {
        // Calendar c = Calendar.getInstance();
        // 创建一个TimePickerDialog实例,并把它显示出来
        // 解释一哈,Activity是context的子类
        new TimePickerDialog(activity, themeResId, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                tv.setText("您选择了:" + hourOfDay + "时" + minute + "分");
            }
        }
                // 设置初始时间

                , calendar.get(Calendar.HOUR_OF_DAY)
                , calendar.get(Calendar.MINUTE)
                // true表示采用24小时制
                , true)
                .show();
    }

最后我们来看最后的使用方法

public class MainActivity extends AppCompatActivity {
    private Button button1, button2, button3;
    DateFormat format = DateFormat.getDateTimeInstance();
    Calendar calendar = Calendar.getInstance(Locale.CHINA);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button1.setOnClickListener(view -> {
            Intent intent = new Intent(this, TestActivity.class);
            startActivity(intent);
        });
        button2.setOnClickListener(v -> {
            //showDatePickerDialog(this, R.style.dialog_date, button2, calendar);
            showDatePickerDialog(this, 6, button2, calendar);
        });
        button3.setOnClickListener(v -> {
            showTimePickerDialog(this, 5, button3, calendar);
        });
    }

补充一下
themeResId参数可供选择的样式很多中,0到6,填入会有不同的显示效果,也可以自定义显示效果