欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用NumberPicker實現(xiàn)滑輪日期選擇器

 更新時間:2023年06月25日 16:58:58   作者:追high  
這篇文章主要為大家介紹了如何使用Android中的NumberPicker控件,以一種簡單而直觀的方式實現(xiàn)滑輪式的日期選擇器,需要的小伙伴可以參考一下

在許多移動應(yīng)用程序中,日期選擇是常見的用戶交互需求。本文將介紹如何使用Android中的NumberPicker控件,以一種簡單而直觀的方式實現(xiàn)滑輪式的日期選擇器。無論您是構(gòu)建日歷應(yīng)用、預(yù)約系統(tǒng)還是其他需要日期選擇的場景,本文將為您提供一個實用的解決方案。

正文

在移動應(yīng)用開發(fā)中,為用戶提供友好、直觀的日期選擇方式至關(guān)重要。NumberPicker是Android平臺上的一個強大工具,它可以幫助我們輕松地實現(xiàn)一個滑輪式的日期選擇器。下面將介紹如何使用NumberPicker來創(chuàng)建一個高度可定制的日期選擇器。

第一步:布局文件中添加NumberPicker

在您的布局文件中,添加一個NumberPicker控件來實現(xiàn)日期選擇的滑輪效果。您可以根據(jù)需要設(shè)置布局參數(shù)、樣式和其他屬性。以下是一個示例:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center">
   <!--年份滑輪-->
    <NumberPicker
        android:id="@+id/yearPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:selectionDividerHeight="0dp" />
    <!--月份滑輪-->
    <NumberPicker
        android:id="@+id/monthPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:selectionDividerHeight="0dp" />
    <!--天數(shù)滑輪-->
    <NumberPicker
        android:id="@+id/dayPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:selectionDividerHeight="0dp" />
</LinearLayout>

其中selectionDividerHeight設(shè)置NumberPicker是否中間有橫線,如果不設(shè)置橫線就給她設(shè)置為“0dp”

第二步:在代碼中初始化和配置NumberPicker

接下來,在代碼中找到NumberPicker控件的引用,并設(shè)置相關(guān)屬性。以下是一些示例代碼,可以根據(jù)您的需求進行定制:

@RequiresApi(Build.VERSION_CODES.Q)
class TimePickerView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
    var yearPicker: NumberPicker
    var monthPicker: NumberPicker
    var dayPicker: NumberPicker
    init {
        LayoutInflater.from(context).inflate(R.layout.time_picker_view, this)
        yearPicker = findViewById(R.id.yearPicker)
        monthPicker = findViewById(R.id.monthPicker)
        dayPicker = findViewById(R.id.dayPicker)
        //設(shè)置NumberPicker不可編輯
        yearPicker.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        monthPicker.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        dayPicker.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        //設(shè)置字體大小
        yearPicker.textSize = 60f
        monthPicker.textSize = 60f
        dayPicker.textSize = 60f
        //設(shè)置不可循環(huán)
        yearPicker.wrapSelectorWheel = false
        val date = Date(System.currentTimeMillis())
        val yearPickerText = SimpleDateFormat("yyyy");// HH:mm:ss
        val monthPickerText = SimpleDateFormat("MM");// HH:mm:ss
        // 設(shè)置年份范圍
        yearPicker.minValue = 1983
        yearPicker.maxValue = 2063
        yearPicker.value = yearPickerText.format(date).toInt()
        // 設(shè)置月份范圍
        val months =
            arrayOf("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月")
        monthPicker.minValue = 1
        monthPicker.maxValue = 12
        monthPicker.displayedValues = months
        monthPicker.value = monthPickerText.format(date).toInt()
        // 設(shè)置日期范圍(根據(jù)年份和月份動態(tài)設(shè)置)
        updateDayPicker(yearPicker.value, monthPicker.value)
        // 監(jiān)聽年份和月份的變化
        yearPicker.setOnValueChangedListener { _, _, _ ->
            updateDayPicker(yearPicker.value, monthPicker.value)
        }
        monthPicker.setOnValueChangedListener { _, _, _ ->
            updateDayPicker(yearPicker.value, monthPicker.value)
        }
        // 監(jiān)聽日期的變化
        dayPicker.setOnValueChangedListener { _, _, dayOfMonth ->
            val selectedDate = "${yearPicker.value}-${monthPicker.value}-$dayOfMonth"
        }
    }
    private fun updateDayPicker(year: Int, month: Int) {
        val calendar = Calendar.getInstance()
        calendar.set(year, month - 1, 1)
        val maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        val date = Date(System.currentTimeMillis())
        val dayPickerText = SimpleDateFormat("dd")
        dayPicker.minValue = 1
        dayPicker.maxValue = maxDay
        dayPicker.value = dayPickerText.format(date).toInt()
    }
}

結(jié)語

使用NumberPicker控件,您可以輕松地實現(xiàn)一個滑輪式的日期選擇器,為用戶提供更好的體驗和交互。

到此這篇關(guān)于Android使用NumberPicker實現(xiàn)滑輪日期選擇器的文章就介紹到這了,更多相關(guān)Android NumberPicker滑輪日期選擇器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android編程實現(xiàn)列表側(cè)滑刪除的方法詳解

    Android編程實現(xiàn)列表側(cè)滑刪除的方法詳解

    這篇文章主要介紹了Android編程實現(xiàn)列表側(cè)滑刪除的方法,結(jié)合實例形式詳細分析了Android列表側(cè)滑刪除功能的原理與具體實現(xiàn)技巧,注釋中包含詳盡的說明,需要的朋友可以參考下
    2018-01-01
  • android控件封裝 自己封裝的dialog控件

    android控件封裝 自己封裝的dialog控件

    自定義dialog肯定是用的很多了 但是感覺每次做都是很亂單純完成任務(wù)而已,現(xiàn)在封裝了一下以后用到直接copy,需要的朋友可以參考下
    2012-11-11
  • Android開發(fā)自定義雙向SeekBar拖動條控件

    Android開發(fā)自定義雙向SeekBar拖動條控件

    這篇文章主要為大家介紹了Android開發(fā)自定義雙向SeekBar拖動條控件使用實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Android實現(xiàn)仿微軟系統(tǒng)加載動畫效果

    Android實現(xiàn)仿微軟系統(tǒng)加載動畫效果

    這篇文章主要介紹了Android實現(xiàn)仿微軟系統(tǒng)加載動畫效果的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android仿硅谷商城實現(xiàn)購物車實例代碼

    Android仿硅谷商城實現(xiàn)購物車實例代碼

    這篇文章主要介紹了Android購物車編輯實現(xiàn),小編覺得挺不錯的,一起跟隨小編過來看看吧
    2018-05-05
  • Android Content Provider詳解及示例代碼

    Android Content Provider詳解及示例代碼

    本文主要講解Android Content Provider,這里提供相關(guān)文檔資料,并附有實現(xiàn)代碼和實現(xiàn)效果圖,有需要的小伙伴可以參考下
    2016-08-08
  • android使用Ultra-PullToRefresh實現(xiàn)下拉刷新自定義代碼

    android使用Ultra-PullToRefresh實現(xiàn)下拉刷新自定義代碼

    本篇文章主要介紹了android使用Ultra-PullToRefresh實現(xiàn)下拉刷新新自定義,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • Android 加載GIF圖最佳實踐方案

    Android 加載GIF圖最佳實踐方案

    最近在項目中遇到需要在界面上顯示一個本地的 GIF 圖的功能,下面通過本文給大家分享Android 加載GIF圖最佳實踐方案,需要的朋友參考下吧
    2017-08-08
  • Android Intent基礎(chǔ)用法及作用詳解

    Android Intent基礎(chǔ)用法及作用詳解

    Intent是一種重要的消息傳遞對象,用于在不同組件(如活動(Activity)、服務(wù)(Service)、廣播接收器(BroadcastReceiver)等)之間進行通信和交互,本文介紹Android Intent基礎(chǔ)用法及作用,感興趣的朋友一起看看吧
    2024-07-07
  • 基于Android實現(xiàn)定時刷新功能

    基于Android實現(xiàn)定時刷新功能

    定時刷新是一種常見的應(yīng)用需求,例如自動加載新數(shù)據(jù)、定時更新 UI、動畫循環(huán)播放、實時監(jiān)控等場景中都需要定時刷新頁面,Android 平臺提供了多種實現(xiàn)定時刷新的方式,本文將結(jié)合實例詳細講解如何實現(xiàn)定時刷新功能,需要的朋友可以參考下
    2025-04-04

最新評論