gpt4 book ai didi

android - Android 中的 TimePickerDialog

转载 作者:太空狗 更新时间:2023-10-29 16:12:08 25 4
gpt4 key购买 nike

我正在尝试在 Android 中实现一个简单的 TimePickerDialog,但出现错误。我正在关注 Google 的本教程:https://developer.android.com/guide/topics/ui/controls/pickers.html

在我的代码中,我有一个名为 setFromTimeButton 的按钮,单击该按钮时应显示带有 TimePicker View 的 DialogFragment,但是,我有一个编译错误:

Cannot resolve symbol TimePickerFragment()

你知道为什么吗?谢谢!

这是我的课:

package it.bitrack.fabio.bitrack;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
//import android.app.Fragment;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;

import android.app.Dialog;
import android.text.format.DateFormat;

import android.app.TimePickerDialog;
import android.app.DatePickerDialog;

import android.icu.util.Calendar;
import android.widget.DatePicker;
import android.widget.TimePicker;

import android.widget.Button;
import android.widget.TextView;


/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ScheduleView.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link ScheduleView#newInstance} factory method to
* create an instance of this fragment.
*/
public class ScheduleView extends DialogFragment implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

Button setFromTimeButton;

android.support.v4.app.FragmentManager fragmentManager;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public ScheduleView() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ScheduleView.
*/
// TODO: Rename and change types and number of parameters
public static ScheduleView newInstance(String param1, String param2) {
ScheduleView fragment = new ScheduleView();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_schedule_view, container, false);

setFromTimeButton = (Button) view.findViewById(R.id.setFromTimeButton);

fragmentManager = getFragmentManager();

setFromTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(fragmentManager, "timePicker");
}
});


return view;
}


// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
}

最佳答案

您需要添加以下类...

    public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}

现在你可以调用它:

 DialogFragment newFragment = new TimePickerFragment();
newFragment.show(fragmentManager, "timePicker");

关于android - Android 中的 TimePickerDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43429196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com