Kotlin显示片段中的TImePickerDialog片段

时间:2018-06-14 18:02:25

标签: android-fragments kotlin datepickerdialog

我正在尝试从片段布局中启动TimePickerDialog片段。 '显示' setOnClickListener中的方法不可用。我错过了一些进口商品吗?或者声明这个newFragment的方法。 TimePickerDialog和TabPagerAdapter之间是否有一些混淆?谢谢,如果你能帮忙的话!

import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button

class AlertsFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_alerts, container, false)

    val timeButton = view.findViewById(R.id.button_timePicker) as Button

    timeButton.setOnClickListener {
        Log.d("mPrint", "button pressed")
        val newFragment = TimePickerFragment()
        newFragment.show(activity.supportFragmentManager, "xx") //SHOW IS NOT AVAILABLE - NONE OF THE FUNCTIONS CALLED WITH THE ARGUMENTS SUPPLIED.
    }

    return view
}

此片段是从MainActivity启动的,它扩展了FragmentActivity()

import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View

class MainActivity : FragmentActivity() {

private lateinit var pagerAdapter: TabPagerAdapter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)

    pagerAdapter = TabPagerAdapter(supportFragmentManager)
    pagerAdapter.addFragments(WorkoutGridFragment(), "Workouts")
    pagerAdapter.addFragments(AlertsFragment(), "Reminders")
    pagerAdapter.addFragments(AboutFragment(), "About")

    // customViewPager is the viewpager created in the activity_main xml layout
    // add the pagerAdapter to the viewpager
    customViewPager.adapter = pagerAdapter

    // set up the viewpager with the tablayout
    customTabLayout.setupWithViewPager(customViewPager)

}
}

这是TimePickerFragment类

import android.app.Dialog
import android.app.DialogFragment // THIS IS WRONG
import android.support.v4.app.DialogFragment // IT SHOULD BE THIS
import android.os.Bundle
import android.widget.TimePicker
import android.app.TimePickerDialog
import android.text.format.DateFormat
import java.util.*

class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

override fun onCreateDialog(savedInstanceState: Bundle): Dialog {

    // use the current time as default values for the picker
    val c = Calendar.getInstance()
    val hour = c.get(Calendar.HOUR_OF_DAY)
    val minute = c.get(Calendar.MINUTE)

    // create a new instance of the TimePicker dialogue and return it
    return TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity())
            )
}

override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
    // Do something with the time chosen by the user
}

}

0 个答案:

没有答案