это снова я. Я делаю школьный проект, и мне помогли с моим приложением для Android. Мне нужно поместить TimePickerDialog внутри фрагмента, в этом фрагменте у меня есть TextView, я хочу указать здесь выбранный час. Но я не могу найти способ сделать это. Когда я делаю Listener, он не работает.
Я попытался изменить getActivity на другую вещь, которая может решить, но это не работает.
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute,
DateFormat.is24HourFormat(getActivity()));
Вот мой основной фрагмент XML:
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.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 = ".DespertarFragment">
<TextView
android:id = "@+id/textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginStart = "83dp"
android:layout_marginEnd = "83dp"
android:text = "Seleccione la hora"
android:textSize = "30sp"
app:layout_constraintBottom_toTopOf = "@+id/button"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "0.0"
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:layout_marginStart = "161dp"
android:layout_marginTop = "1dp"
android:layout_marginEnd = "162dp"
android:text = "Poner"
app:layout_constraintBottom_toTopOf = "@+id/button_cancel"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "1.0"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/textView1" />
<Button
android:id = "@+id/button_cancel"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginStart = "161dp"
android:layout_marginTop = "145dp"
android:layout_marginEnd = "162dp"
android:layout_marginBottom = "156dp"
android:text = "Cancelar"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/button" />
</android.support.constraint.ConstraintLayout>
Вот класс:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_despertar, container, false);
Button poner = (Button) view.findViewById(R.id.button);
poner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getFragmentManager(), "Timepo");
}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText("Hora: " + hourOfDay + minute);
}
А вот мой класс TimePickerDialog:
public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), HERE I DON'T KNOW WHAT CAN I PUT HERE TO PASS THE TIME TO MY MAIN FRAGMENT, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
}
ПРИМЕЧАНИЕ. Мне не нужен onTimeSet в классе TimePickerDialog, мне нужно время в основном фрагменте.
Я ожидаю, что ViewText со временем, но это ничего не изменило.
в TimePickerFragment поместите код onCreateDialog() в onCreateView.
Попробуйте это, это простой способ показать средство выбора времени
private void timePick(final TextView textView) {
TimePickerDialog.OnTimeSetListener timer = new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//set time in your text view
}
};
new TimePickerDialog(this,timer,currenthour,currentMinuts,false).show();
}
вызовите этот метод
poner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timePick(yourextView);
}
});
Омг, спасибо! Я много пробовал, и это идеально, я не понимал, что не могу этого сделать.
Он говорит мне о несовместимых типах в возврате.