如何解决无法在片段中显示日期选择器对话框
我正在尝试在插入片段中实现一个日期选择器对话框。在插入片段中,我添加了一个按钮来显示日期选择器对话框并在文本视图中显示所选日期。
问题
Type mismatch: inferred type is AddFragment but Context was expected
Type mismatch: inferred type is String but Editable? was expected
在 Add Fragment.kt 文件中,
它在 this
函数中的 view
& dobDatePicker()
关键字显示红色下划线。
注意:我使用 view binding
来调用 UI id。
这是fragment_add.xml
的代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:fillViewport="true"
tools:context=".fragments.add.AddFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- NSU ID INPUT-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/nsu_id"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/nsu_id"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/selected_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:inputType="number"
android:maxLength="7"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
android:typeface="normal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Student name-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/student_name"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/student_name"
android:orientation="vertical"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nsu_id">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:inputType="textPersonName"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
android:typeface="normal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- School list-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/school_list_dropdown"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/select_academic_school"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/student_name">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
tools:ignore="LabelFor" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Department list-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/department_list_dropdown"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/department_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/school_list_dropdown">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
tools:ignore="LabelFor" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Date of birth text field-->
<com.google.android.material.textview.MaterialTextView
android:id="@id/selected_date"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="300dp"
android:layout_height="52dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/roboto"
android:gravity="center_vertical"
android:hint="@string/date_of_birth"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
android:typeface="normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/department_list_dropdown" />
<!-- date picker button -->
<com.google.android.material.button.MaterialButton
android:id="@+id/datePicker_button"
android:layout_width="52dp"
android:layout_height="52dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:icon="@drawable/ic_outline_calendar_today_24"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:layout_constraintBottom_toBottomOf="@+id/selected_date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/selected_date"
app:layout_constraintTop_toTopOf="@+id/selected_date"
app:layout_constraintVertical_bias="1.0" />
<!-- Phone number-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phone_number"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/phone_number"
android:orientation="vertical"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/selected_date">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:inputType="phone"
android:maxLength="11"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
android:typeface="normal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- NID number-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/nid_number"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/nid_number"
android:orientation="vertical"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone_number">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:inputType="number"
android:maxLength="10"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
android:typeface="normal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Present Address-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/present_address"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/present_address"
android:orientation="vertical"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nid_number">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:inputType="textPostalAddress"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
android:typeface="normal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Permanent Address-->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/permanent_address"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/permanent_address"
android:orientation="vertical"
app:endIconMode="clear_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/present_address">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:inputType="textPostalAddress"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
android:typeface="normal" />
</com.google.android.material.textfield.TextInputLayout>
<!-- Submit button -->
<com.google.android.material.button.MaterialButton
android:id="@+id/submit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="@font/roboto"
android:text="@string/submit"
android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Button"
android:typeface="normal"
app:elevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/permanent_address" />
<!--Constraint layout finish-->
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
这是AddFragment.kt
的代码:
package com.example.studentinfodb.fragments.add
import android.app.DatePickerDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.fragment.app.Fragment
import com.example.studentinfodb.R
import com.example.studentinfodb.databinding.FragmentAddBinding
import java.util.*
class AddFragment : Fragment() {
private var _binding: FragmentAddBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentAddBinding.inflate(inflater,container,false)
val view = binding.root
/*
Academic school dropdown lists with array adapter
*/
val schoolNames = listof(
"School of Business & Economics","School of Humanities & Social " +
"Sciences","School of Engineering & Physical Sciences","School of Health & Life Sciences"
)
val schoolAdapter = ArrayAdapter(requireContext(),R.layout.school_list,schoolNames)
(binding.schoolListDropdown.editText as? AutoCompleteTextView)?.setAdapter(schoolAdapter)
/*
Department names dropdown lists with array adapter
*/
val departmentNames = listof(
"Accounting & Finance","Economics","Management","Marketing & International Business","MBA & EMBA Programs"
)
val departmentAdapter =
ArrayAdapter(requireContext(),departmentNames)
(binding.departmentListDropdown.editText as? AutoCompleteTextView)?.setAdapter(
departmentAdapter
)
/*
Date picker for date of birth
*/
binding.datePickerButton.setonClickListener {
dobDatePicker()
}
return view
}
private fun dobDatePicker() {
/*
pick date from calender
*/
val myCalender = Calendar.getInstance()
val year = myCalender.get(Calendar.YEAR)
val month = myCalender.get(Calendar.MONTH)
val day = myCalender.get(Calendar.DAY_OF_MONTH)
// open the dialog
val datePicker = DatePickerDialog(
this,DatePickerDialog.OnDateSetListener { view,selectedYear,selectedMonth,selectedDayOfMonth ->
// date format
val selectedDate = "$selectedDayOfMonth/ ${selectedMonth + 1}/ $selectedYear"
// show the date in text view
binding.selectedDate.text = selectedDate
},year,month,day
)
// restriction for the future date selection
datePicker.datePicker.maxDate = Date().time - 86400000
// visible the date picker dialog
datePicker.show()
}
}
这是MainActivity.kt
的代码:
package com.example.studentinfodb
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.setupActionBarWithNavController
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*
Change the name of the action bar
*/
setupActionBarWithNavController(findNavController(R.id.fragment))
}
}
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。