亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

正文

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

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

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

<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

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

@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ù)年份和月份動(dòng)態(tài)設(shè)置)
        updateDayPicker(yearPicker.value, monthPicker.value)
        // 監(jiān)聽(tīng)年份和月份的變化
        yearPicker.setOnValueChangedListener { _, _, _ ->
            updateDayPicker(yearPicker.value, monthPicker.value)
        }
        monthPicker.setOnValueChangedListener { _, _, _ ->
            updateDayPicker(yearPicker.value, monthPicker.value)
        }
        // 監(jiān)聽(tīng)日期的變化
        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é)語(yǔ)

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

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

相關(guān)文章

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

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

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

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

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

    Android開(kāi)發(fā)自定義雙向SeekBar拖動(dòng)條控件

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

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

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

    Android仿硅谷商城實(shí)現(xiàn)購(gòu)物車(chē)實(shí)例代碼

    這篇文章主要介紹了Android購(gòu)物車(chē)編輯實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Android Content Provider詳解及示例代碼

    Android Content Provider詳解及示例代碼

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

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

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

    Android 加載GIF圖最佳實(shí)踐方案

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

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

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

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

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

最新評(píng)論