2017-09-30 71 views
0

我有一個日期選擇器對話框。我只想顯示日和月。年份選取器對話框必須隱藏。我已經嘗試其他答案,如this。沒有任何作品適合我。它也應該支持Kitkat到牛軋糖設備。我的日期選擇器代碼如下。如何在Kotlin(Android)的日期選擇器對話框中僅顯示日期和月份?

 fun setDatePickerDialog() { 
     mDobDialog = DatePickerDialog([email protected], R.style.VDDatePickerDialogTheme, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> 
      val newDate = Calendar.getInstance() 
      newDate.set(year, monthOfYear, dayOfMonth) 
      //dob_textview.setText("($dayOfMonth) ($monthOfYear) ($year)") 
      val dateFormat = SimpleDateFormat(VDAppConstants.DOB_DISPLAY_FORMAT) 
      dob_textview?.setText(dateFormat.format(newDate.time)) 

     }, mNewCalendar.get(Calendar.YEAR), mNewCalendar.get(Calendar.MONTH), mNewCalendar.get(Calendar.DAY_OF_MONTH)) 

     mNewCalendar.set(1978, 
       mNewCalendar.get(Calendar.MONTH), 
       mNewCalendar.get(Calendar.DAY_OF_MONTH)) 
     mDobDialog?.datePicker?.maxDate = mNewCalendar.timeInMillis 
    } 

以下代碼僅適用於Kitkat設備,但不適用於牛軋糖設備。

val mDobDialog = DatePickerDialog([email protected], android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> 
     // Display Selected date in textbox 
     //date.setText("" + dayOfMonth + " " + monthOfYear + ", " + year) 
    }, year, month, day) 
    mDobDialog.show() 


    // Hide Year Selector in Date Picker 
    mDobDialog.findViewById(Resources.getSystem().getIdentifier("year", "id", "android")).visibility = View.GONE 

回答

1

我測試過不同的代碼,這段代碼工作正常。如果你使用datepicker主題作爲Theme_Holo_Dialog,那麼它工作正常。工作代碼如下。

注:它不工作,如果你設置的主題Theme_Material_Dialog

package com.wave18.datepickedialogdemo 

import android.annotation.SuppressLint 
import android.app.DatePickerDialog 
import android.content.res.Resources 
import android.os.Bundle 
import android.support.v7.app.AppCompatActivity 
import android.view.View 
import kotlinx.android.synthetic.main.activity_main.* 
import java.util.* 

class MainActivity : AppCompatActivity() { 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     setContentView(R.layout.activity_main) 

     // Date Picker Dialog 
     val dialog = datePickerDialog() 

     // Button for Showing Date Picker Dialog 
     button_show_date_picker.setOnClickListener { 

      // Show Date Picker 
      dialog.show() 

      // Hide Year Selector 
      val year = dialog.findViewById<View>(Resources.getSystem().getIdentifier("android:id/year", null, null)) 
      if (year != null) { 
       year.visibility = View.GONE 
      } 


     } 


    } 

    // Function for Showing Date Picker 
    @SuppressLint("SetTextI18n") 
    fun datePickerDialog(): DatePickerDialog { 
     val c = Calendar.getInstance() 
     val year = c.get(Calendar.YEAR) 
     val month = c.get(Calendar.MONTH) 
     val day = c.get(Calendar.DAY_OF_MONTH) 

     // Date Picker Dialog 
     val datePickerDialog = DatePickerDialog([email protected], android.R.style.Theme_Holo_Dialog, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> 
      // Display Selected date in textbox 
      date.text = "$dayOfMonth $monthOfYear, $year" 
     }, year, month, day) 
     // Show Date Picker 

     return datePickerDialog 


    } 

} 

Date Picker Dialog without year selector

+0

如何從日期選擇器獲得一年場?你能否請加一些代碼 –

+0

@VinayakB我更新了答案看看。 –

+0

我正在使用datepicker對話框 –

相關問題