我刚刚开始学习Kotlin,我目前的目标是在按下按钮后在片段中创建一个简单的日期选择器。
作为指导原则,我使用了这个例子,由pickers的android开发人员文档显示。作为模板,我使用了底部导航标准模板。
我的问题:根据这个例子,我创建了一个类DatePickerFragment
。在返回行中,引用了一个需要上下文作为输入的方法。在示例中,写入了CCD_ 2。我不知道在这里实现什么才能使代码正常工作。
错误消息(参考类DatePickerFragment(:
- "nCreateDialog"不覆盖任何内容
- 类型不匹配:推断的类型是FragmentActivity?但需要上下文
在下面的文章中,我粘贴了一些代码。整个代码也可以在这里找到:https://github.com/andregreschat/DatePickerinFragmentTest
DatePickerFragment类:
package de.datepickerinfragmenttest
import android.app.DatePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.DatePicker
import androidx.fragment.app.DialogFragment
import java.util.*
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
// Use the current date as the default date in the picker
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
// Create a new instance of DatePickerDialog and return it
return DatePickerDialog(activity, this, year, month, day)
}
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
// Do something with the date chosen by the user
}
}
主要活动
package de.datepickerinfragmenttest
import android.os.Bundle
import android.view.View
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
fun showDatePickerDialog(v: View) {
val newFragment = DatePickerFragment()
newFragment.show(supportFragmentManager, "datePicker")
}
}
}
片段:
package de.datepickerinfragmenttest.ui.home
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import de.datepickerinfragmenttest.DatePickerFragment
import de.datepickerinfragmenttest.R
import androidx.fragment.app.FragmentManager
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}
}
带按钮的片段布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SetDate"
app:layout_constraintBottom_toTopOf="@+id/text_home"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="showDatePickerDialog"/>
</androidx.constraintlayout.widget.ConstraintLayout>
我有点想知道您正在设置navigation
组件,但却没有使用它来移动到DialogFragment
。
在实例化对话框时,您可能需要调用另外两种方法(而不是activity
属性(来获得所需的Context
:
requireContext()
requireActivity()
(如果需要确保活动Context
已通过(