我需要将 DialogFragment 放入 Fragment 中
I need to put a DialogFragment inside a Fragment
又是我。我正在做一个学校项目,我的应用程序在 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>
这是 class:
@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 class:
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()));
}
}
注意:我不需要 TimePickerDialog 中的 onTimeSet class,我需要主片段中的时间。
我希望 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);
}
});
又是我。我正在做一个学校项目,我的应用程序在 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>
这是 class:
@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 class:
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()));
}
}
注意:我不需要 TimePickerDialog 中的 onTimeSet class,我需要主片段中的时间。
我希望 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);
}
});