最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇

 更新時(shí)間:2022年09月19日 11:00:14   作者:informationchina  
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇的具體代碼,供大家參考,具體內(nèi)容如下

前言

整合Android原生控件(日期控件DatePicker、時(shí)間控件TimePicker)實(shí)現(xiàn)選擇日期、時(shí)間綁定。

本文僅僅是一種參考,不光是時(shí)間控件,自定義的Layout一樣可以采用這種方式。

涉及技術(shù)要點(diǎn):

1.控件事件綁定
2.彈出框AlertDialog
3.日期格式化SimpleDateFormat

一、創(chuàng)建彈出Layout

1.1 新建Layout,修改樣式為L(zhǎng)inearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:orientation="vertical">


</LinearLayout>

1.2 Layout中添加日期和時(shí)間控件

注意需要將控件的calendarViewShown指定為false及datePickerMode屬性指定為spinner

<?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="wrap_content"
? ? android:orientation="vertical">

? ? <DatePicker
? ? ? ? android:id="@+id/dialog_datetime_date_picker"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:calendarViewShown="false"
? ? ? ? android:datePickerMode="spinner"/>

? ? <TimePicker
? ? ? ? android:id="@+id/dialog_datetime_time_picker"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:calendarViewShown="false"
? ? ? ? android:datePickerMode="spinner" />

</LinearLayout>

二、新建DateTimeDialog

創(chuàng)建DateTimeDialog類是為了將方法封裝,以便我項(xiàng)目多次調(diào)用

public class DateTimeDialog {

}

2.1 創(chuàng)建靜態(tài)方法

2.1.1 創(chuàng)建SetDateDialog,用于選擇日期

public static void SetDateDialog(Context context, Activity activity, TextView textView, String... title) {
}

這里我們將引用控件的context、activity作為參數(shù)傳入方法中,方便我們動(dòng)態(tài)加載Layout和指定AlertDialog彈出所在的Activity,避免彈出框無(wú)法顯示。
textView參數(shù)為需要綁定選擇的控件,并且在選擇之后,會(huì)將選擇的日期返回給textView
titile是可選參數(shù),指定彈出框的標(biāo)題,不指定的話,會(huì)默認(rèn)為“選擇日期”

2.1.2 SetDateDialog中綁定textView的click事件

給textView綁定事件后,在用戶點(diǎn)擊控件時(shí)即可執(zhí)行相應(yīng)的事件內(nèi)容,在此我們需要的是用戶點(diǎn)擊控件時(shí),彈出日期選擇框。

textView.setOnClickListener(view -> {
?? ?AlertDialog.Builder builder = new AlertDialog.Builder(activity);
?? ?builder.setView(view1);
?? ?builder.create().show();
});

由于我們的控件中含有時(shí)間控件,需要隱藏

View view1 = LayoutInflater.from(context).inflate(R.layout.dialog_datetime, null);
? final TimePicker timePicker = view1.findViewById(R.id.dialog_datetime_time_picker);
? ? ? ? ? ? ? ? ? ? ? ? timePicker.setVisibility(View.GONE);

如果textView有默認(rèn)值,則在彈出的時(shí)候需要將textView的日期帶入彈出框中

final DatePicker datePicker = view1.findViewById(R.id.dialog_datetime_date_picker);
? ? ? ? ? ? Calendar calendar;
? ? ? ? ? ? String strDate = textView.getText().toString();
? ? ? ? ? ? calendar = convertDateToCalendar(strDate);
? ? ? ? ? ? datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), null);

設(shè)置彈出框的標(biāo)題

if (title != null && title.length > 0) {
? ? ? ? ? ? ? ? builder.setTitle(title[0]);
? ? ? ? ? ? } else
? ? ? ? ? ? ? ? builder.setTitle("選擇日期");

實(shí)現(xiàn)彈出框的按鈕事件:

點(diǎn)擊確定時(shí),綁定值給textView,并關(guān)閉彈窗;點(diǎn)擊取消時(shí),直接關(guān)閉天窗;點(diǎn)擊現(xiàn)在時(shí),將當(dāng)前時(shí)間傳給textView,并關(guān)閉彈窗。

builder.setPositiveButton("確 定", (dialog, i) -> {
? ? ? ? ? ? ? ? //日期格式
? ? ? ? ? ? ? ? int year = datePicker.getYear();
? ? ? ? ? ? ? ? int month = datePicker.getMonth() + 1;
? ? ? ? ? ? ? ? int dayOfMonth = datePicker.getDayOfMonth();

? ? ? ? ? ? ? ? textView.setText(String.format(Locale.getDefault(), "%d年%d月%d日 ", year, month, dayOfMonth));
? ? ? ? ? ? ? ? dialog.cancel();
? ? ? ? ? ? });
? ? ? ? ? ? builder.setNegativeButton("取 消", (dialog, which) -> dialog.cancel());
? ? ? ? ? ? builder.setNeutralButton("現(xiàn) 在", (dialog, i) -> {
? ? ? ? ? ? ? ? SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月d日",Locale.getDefault());// HH:mm:ss
? ? ? ? ? ? ? ? //獲取當(dāng)前時(shí)間
? ? ? ? ? ? ? ? Date date = new Date(System.currentTimeMillis());
? ? ? ? ? ? ? ? textView.setText(simpleDateFormat.format(date));
? ? ? ? ? ? ? ? dialog.cancel();
? ? ? ? ? ? });

以下是完整代碼:

public static void SetDateDialog(Context context, Activity activity, TextView textView, String... title) {
? ? ? ? textView.setOnClickListener(view -> {
? ? ? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(activity);
? ? ? ? ? ? View view1 = LayoutInflater.from(context).inflate(R.layout.dialog_datetime, null);
? ? ? ? ? ? final DatePicker datePicker = view1.findViewById(R.id.dialog_datetime_date_picker);
? ? ? ? ? ? final TimePicker timePicker = view1.findViewById(R.id.dialog_datetime_time_picker);
? ? ? ? ? ? timePicker.setIs24HourView(true);

? ? ? ? ? ? Calendar calendar;
? ? ? ? ? ? String strDate = textView.getText().toString();
? ? ? ? ? ? calendar = convertDateToCalendar(strDate);
? ? ? ? ? ? datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), null);
? ? ? ? ? ? timePicker.setHour(calendar.get(Calendar.HOUR_OF_DAY));
? ? ? ? ? ? timePicker.setMinute(calendar.get(Calendar.MINUTE));

? ? ? ? ? ? timePicker.setVisibility(View.GONE);
// ? ? ? ? ? ?datePicker.setCalendarViewShown(false);
? ? ? ? ? ? //設(shè)置Date布局
? ? ? ? ? ? builder.setView(view1);
? ? ? ? ? ? if (title != null && title.length > 0) {
? ? ? ? ? ? ? ? builder.setTitle(title[0]);
? ? ? ? ? ? } else
? ? ? ? ? ? ? ? builder.setTitle("選擇日期");

? ? ? ? ? ? builder.setPositiveButton("確 定", (dialog, i) -> {
? ? ? ? ? ? ? ? //日期格式
? ? ? ? ? ? ? ? int year = datePicker.getYear();
? ? ? ? ? ? ? ? int month = datePicker.getMonth() + 1;
? ? ? ? ? ? ? ? int dayOfMonth = datePicker.getDayOfMonth();

? ? ? ? ? ? ? ? textView.setText(String.format(Locale.getDefault(), "%d年%d月%d日 ", year, month, dayOfMonth));
? ? ? ? ? ? ? ? dialog.cancel();
? ? ? ? ? ? });
? ? ? ? ? ? builder.setNegativeButton("取 消", (dialog, which) -> dialog.cancel());
? ? ? ? ? ? builder.setNeutralButton("現(xiàn) 在", (dialog, i) -> {
? ? ? ? ? ? ? ? SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月d日",Locale.getDefault());// HH:mm:ss
? ? ? ? ? ? ? ? //獲取當(dāng)前時(shí)間
? ? ? ? ? ? ? ? Date date = new Date(System.currentTimeMillis());
? ? ? ? ? ? ? ? textView.setText(simpleDateFormat.format(date));
? ? ? ? ? ? ? ? dialog.cancel();
? ? ? ? ? ? });
? ? ? ? ? ? builder.create().show();
? ? ? ? });

? ? }

同樣的方式我們?cè)賹?shí)現(xiàn)選擇日期時(shí)間的方法,具體不再贅述,上代碼:

public static void SetDateTimeDialog(Context context, Activity activity, TextView textView, String... title) {
? ? ? ? textView.setOnClickListener(view -> {
? ? ? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(activity);
? ? ? ? ? ? View view1 = LayoutInflater.from(context).inflate(R.layout.dialog_datetime, null);
? ? ? ? ? ? final DatePicker datePicker = view1.findViewById(R.id.dialog_datetime_date_picker);
? ? ? ? ? ? final TimePicker timePicker = view1.findViewById(R.id.dialog_datetime_time_picker);
? ? ? ? ? ? timePicker.setIs24HourView(true);
// ? ? ? ? ? ?datePicker.setCalendarViewShown(false);

? ? ? ? ? ? Calendar calendar;
? ? ? ? ? ? String strDate = textView.getText().toString();
? ? ? ? ? ? calendar = convertDateToCalendar(strDate);
? ? ? ? ? ? datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), null);
? ? ? ? ? ? timePicker.setHour(calendar.get(Calendar.HOUR_OF_DAY));
? ? ? ? ? ? timePicker.setMinute(calendar.get(Calendar.MINUTE));

? ? ? ? ? ? //設(shè)置Date布局
? ? ? ? ? ? builder.setView(view1);
? ? ? ? ? ? if (title != null && title.length > 0) {
? ? ? ? ? ? ? ? builder.setTitle(title[0]);
? ? ? ? ? ? } else
? ? ? ? ? ? ? ? builder.setTitle("選擇時(shí)間");

? ? ? ? ? ? builder.setPositiveButton("確 定", (dialog, i) -> {
? ? ? ? ? ? ? ? //日期格式
? ? ? ? ? ? ? ? int year = datePicker.getYear();
? ? ? ? ? ? ? ? int month = datePicker.getMonth() + 1;
? ? ? ? ? ? ? ? int dayOfMonth = datePicker.getDayOfMonth();

? ? ? ? ? ? ? ? int hour = timePicker.getHour();
? ? ? ? ? ? ? ? int min = timePicker.getMinute();
// ? ? ? ? ? ? ? ? ? ?timePicker.getSecond();
? ? ? ? ? ? ? ? textView.setText(String.format(Locale.getDefault(), "%d年%d月%d日 %d:%d", year, month, dayOfMonth, hour, min));
? ? ? ? ? ? ? ? dialog.cancel();
? ? ? ? ? ? });
? ? ? ? ? ? builder.setNegativeButton("取 消", (dialog, which) -> dialog.cancel());
? ? ? ? ? ? builder.setNeutralButton("現(xiàn) 在", (dialog, i) -> {
? ? ? ? ? ? ? ? SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月d日 HH:mm", Locale.getDefault());// HH:mm:ss
? ? ? ? ? ? ? ? //獲取當(dāng)前時(shí)間
? ? ? ? ? ? ? ? Date date = new Date(System.currentTimeMillis());
? ? ? ? ? ? ? ? textView.setText(simpleDateFormat.format(date));
? ? ? ? ? ? ? ? dialog.cancel();
? ? ? ? ? ? });
? ? ? ? ? ? builder.create().show();
? ? ? ? });
? ? }

文中提到的convertDateToCalendar是筆者用于日期轉(zhuǎn)換的,您可以根據(jù)自己的需要去靈活實(shí)現(xiàn)

private static Calendar convertDateToCalendar(String strDate) {
? ? ? ? int year;
? ? ? ? int month;
? ? ? ? int day;
? ? ? ? int hour;
? ? ? ? int minute;
? ? ? ? Calendar calendar = Calendar.getInstance();

? ? ? ? //獲取當(dāng)前時(shí)間
? ? ? ? Date date = new Date(System.currentTimeMillis());

? ? ? ? calendar.setTime(date);
// ? ? ? ?calendar.add(Calendar.MONTH,1);
? ? ? ? year = calendar.get(Calendar.YEAR);
? ? ? ? month = calendar.get(Calendar.MONTH);
? ? ? ? day = calendar.get(Calendar.DATE);
? ? ? ? hour = calendar.get(Calendar.HOUR_OF_DAY);
? ? ? ? minute = calendar.get(Calendar.MINUTE);

? ? ? ? if (strDate != null && !strDate.equals("")) {
? ? ? ? ? ? if (strDate.contains(":")) {
? ? ? ? ? ? ? ? strDate = strDate.split(":")[1];
? ? ? ? ? ? }


? ? ? ? ? ? strDate = strDate.replace("年", "-").replace("月", "-").replace("日", "").replace(".", "").replace(" ", "-").replace(":", "-");
? ? ? ? ? ? Log.d("liuwz", "convertDateToCalendar: "+strDate);
? ? ? ? ? ? if (strDate.split("-").length >= 3) {
? ? ? ? ? ? ? ? year = Integer.parseInt(strDate.split("-")[0]);
? ? ? ? ? ? ? ? month = Integer.parseInt(strDate.split("-")[1]);
? ? ? ? ? ? ? ? day = Integer.parseInt(strDate.split("-")[2]);
? ? ? ? ? ? ? ? if (strDate.split("-").length >= 5) {
? ? ? ? ? ? ? ? ? ? hour = Integer.parseInt(strDate.split("-")[3]);
? ? ? ? ? ? ? ? ? ? minute = Integer.parseInt(strDate.split("-")[4]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? calendar.set(year, month, day, hour, minute);
? ? ? ? ? ? ? ? calendar.add(Calendar.MONTH, -1);
? ? ? ? ? ? } else if (strDate.split("-").length >= 2) {
? ? ? ? ? ? ? ? hour = Integer.parseInt(strDate.split("-")[0]);
? ? ? ? ? ? ? ? minute = Integer.parseInt(strDate.split("-")[1]);
? ? ? ? ? ? ? ? calendar.set(year, month, day, hour, minute);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? return calendar;
? ? }

至此已經(jīng)大功告成,下面看下如何引用

2.2 引用

在任意需要用到選擇時(shí)間的Activity的onCreate方法中添加下面一句代碼即可:

DateTimeDialog.SetDateDialog(getApplicationContext(), MainActivity.this, timeSelectView, "請(qǐng)選擇日期");

將其中的MainActivity修改為您當(dāng)前的Activity;將timeSelectView 修改為您頁(yè)面中的時(shí)間TextView或者EditView,“請(qǐng)選擇日期”為可選參數(shù),可忽略。

三. 總結(jié)

本文僅僅為了使用方便而對(duì)AlertDialog進(jìn)行了封裝,用的是Android的原生控件,寫在此處僅僅給新入門的朋友們以參考。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Android Service 生命周期的詳細(xì)介紹

    基于Android Service 生命周期的詳細(xì)介紹

    本篇文章小編為大家介紹,基于Android Service 生命周期的詳解。需要的朋友參考下
    2013-04-04
  • Android開發(fā)使用RecyclerView添加點(diǎn)擊事件實(shí)例詳解

    Android開發(fā)使用RecyclerView添加點(diǎn)擊事件實(shí)例詳解

    這篇文章主要為大家介紹了Android開發(fā)使用RecyclerView添加點(diǎn)擊事件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 谷歌被屏蔽后如何搭建安卓環(huán)境

    谷歌被屏蔽后如何搭建安卓環(huán)境

    從5月27日開始,谷歌(Google)在華的幾乎所有的服務(wù)都處于無(wú)法使用的狀態(tài),除了搜索引擎遭到屏蔽之外,谷歌的郵箱(Gmail)、日歷(Calendar)、翻譯(Translate)、地圖(Maps)、分析(Analytics)和Google AdSense等產(chǎn)品也受到了影響。同時(shí)安裝安卓環(huán)境的時(shí)候同樣容易出現(xiàn)問題
    2014-06-06
  • Android ListView實(shí)現(xiàn)下拉加載功能

    Android ListView實(shí)現(xiàn)下拉加載功能

    這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)下拉加載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android自定義DataGridView數(shù)據(jù)表格控件

    Android自定義DataGridView數(shù)據(jù)表格控件

    這篇文章主要介紹了Android自定義DataGridView數(shù)據(jù)表格控件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android插件化之資源動(dòng)態(tài)加載

    Android插件化之資源動(dòng)態(tài)加載

    這篇文章主要介紹了Android插件化之資源動(dòng)態(tài)加載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android Studio不能獲取遠(yuǎn)程依賴包的完美解決方法

    Android Studio不能獲取遠(yuǎn)程依賴包的完美解決方法

    這篇文章主要介紹了Android Studio不能獲取遠(yuǎn)程依賴包的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-11-11
  • Android多返回棧技術(shù)

    Android多返回棧技術(shù)

    本文將詳情講解用戶通過系統(tǒng)返回按鈕導(dǎo)航回去的一組頁(yè)面,在開發(fā)中被稱為返回棧 (back stack)。多返回棧即一堆 "返回棧",對(duì)多返回棧的支持是在 Navigation 2.4.0-alpha01 和 Fragment 1.4.0-alpha01 中開始的,有興趣的話一起參與學(xué)習(xí)
    2021-08-08
  • Android使用DisplayManager創(chuàng)建虛擬屏流程及原理解析

    Android使用DisplayManager創(chuàng)建虛擬屏流程及原理解析

    這篇文章主要介紹了Android使用DisplayManager創(chuàng)建虛擬屏流程及原理解析,DisplayManager提供了createVirtualDisplay接口,用于創(chuàng)建虛擬屏,虛擬屏可以把屏幕分出不同的區(qū)域,需要的朋友可以參考下
    2024-05-05
  • Android利用碎片fragment實(shí)現(xiàn)底部標(biāo)題欄(Github模板開源)

    Android利用碎片fragment實(shí)現(xiàn)底部標(biāo)題欄(Github模板開源)

    Fragment可以作為Activity的組成部分,一個(gè)Activity可以有多個(gè)Fragment,這篇文章主要介紹了Android利用碎片fragment實(shí)現(xiàn)底部標(biāo)題欄(Github模板開源),需要的朋友可以參考下
    2019-12-12

最新評(píng)論

徐闻县| 林口县| 汝城县| 香河县| 德清县| 平凉市| 兴安县| 渑池县| 胶南市| 高淳县| 宁阳县| 富阳市| 察雅县| 杭锦旗| 伊宁县| 松溪县| 江源县| 新蔡县| 长春市| 江永县| 靖西县| 霍林郭勒市| 瑞金市| 遵义县| 杂多县| 吴川市| 嘉禾县| 东乡族自治县| 庐江县| 赞皇县| 砀山县| 保定市| 石楼县| 石嘴山市| 施秉县| 望城县| 巧家县| 禄劝| 呼图壁县| 磐安县| 越西县|