我正在尝试在时间选择器对话框中放置一个复选框,位于时间选择器上方,只要它被选中,时间选择器就可以工作。
我尝试在 TimePickerFragment(onCreateDialog) 中制作 CheckBox,并将复选框放在主活动中,而不是在时间对话框中。TimePickerFragment:
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
private MyAlarmReceiver alarm;
//final CheckBox chk=null;
@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);
final CheckBox chk = (CheckBox) getActivity().findViewById(R.id.chk);
if (chk.isChecked()) {
chk.setChecked(false);
}
// 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) {
Context con = getActivity();
MainActivity.hour = hourOfDay;
MainActivity.min = minute;
//Toast.makeText(con, "TimeGet: "+MainActivity.hour+","+MainActivity.min, Toast.LENGTH_LONG).show();
alarm = new MyAlarmReceiver();
if(alarm != null){alarm.setOnetimeTimer(con);}
else
Toast.makeText(con, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
Main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
android:background="@drawable/back" >
<CheckBox
android:id="@+id/chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk"
android:checked="true"/>
</RelativeLayout>
知道为什么吗?谢谢!