我有一个打开自定义对话框的开关。 在onCheckedChanged()方法中,我实现了表示对话框的类并调用方法show()。问题是只有在onCheckedChanged()结束后才会打开对话框。我试着在对话框的类的构造函数中调用show(),但同样的事情发生了。
该对话框供用户设置提醒的时间和日期。在对话框中还有三个按钮“OK”,“Cancel”和“Reset”。问题是,如果用户单击“取消”,我想将开关更改为“关闭”,但只有在onCheckedChanged()结束后才打开对话框,当我单击“取消”时,对话框才会被解除,并且开关保持“开”。
对话框类:
public class DateTimePickerDialog extends Dialog implements OnDateChangedListener, OnTimeChangedListener, View.OnClickListener
{
private Calendar calendar;
private DatePicker datePicker;
private TimePicker timePicker;
private ViewSwitcher viewSwitcher;
private Button btnSwitchToTime;
private Button btnSwitchToDate;
private Button btnOk;
private Button btnReset;
private Button btnCancel;
private boolean reminderFlag;
/**
* Constructor
* @param context
*/
public DateTimePickerDialog(Context context)
{
super(context);
setContentView(R.layout.dialog_datetimepicker);
setTitle(R.string.title_dialog_datetimepicker);
// set the calendar with the current date
calendar = Calendar.getInstance();
calendar.getTime();
// set the reminder flag. this flag is for setting the ToggleButton in the parent activity
reminderFlag = true;
// initialize the date picker and the time picker with current time and date
datePicker = (DatePicker) findViewById(R.id.picker_DatePicker);
timePicker = (TimePicker) findViewById(R.id.picker_TimePicker);
resetDateTime();
// set listener for the time picker
timePicker.setOnTimeChangedListener(this);
// populate the view switcher
viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher_DateTimePicker);
// populate and set listeners for all the buttons
btnSwitchToTime = (Button) findViewById(R.id.button_SwitchToTime);
btnSwitchToTime.setOnClickListener(this);
btnSwitchToDate = (Button) findViewById(R.id.button_SwitchToDate);
btnSwitchToDate.setOnClickListener(this);
btnOk = (Button) findViewById(R.id.button_Ok);
btnOk.setOnClickListener(this);
btnReset = (Button) findViewById(R.id.button_Reset);
btnReset.setOnClickListener(this);
btnCancel = (Button) findViewById(R.id.button_Cancel);
btnCancel.setOnClickListener(this);
this.show();
}
@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.button_SwitchToTime:
btnSwitchToTime.setEnabled(false);
findViewById(R.id.button_SwitchToDate).setEnabled(true);
viewSwitcher.showNext();
break;
case R.id.button_SwitchToDate:
btnSwitchToDate.setEnabled(false);
findViewById(R.id.button_SwitchToTime).setEnabled(true);
viewSwitcher.showPrevious();
break;
case R.id.button_Cancel:
reminderFlag = false;
dismiss();
break;
case R.id.button_Ok:
dismiss();
break;
case R.id.button_Reset:
resetDateTime();
break;
}
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
}
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
calendar.set(year, monthOfYear, dayOfMonth, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
}
/**
* Reset DatePicker, TimePicker and the member calendar
*/
public void resetDateTime()
{
calendar = Calendar.getInstance();
calendar.getTime();
datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), this);
//datePicker.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
timePicker.setCurrentHour(calendar.get(Calendar.HOUR));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
/**
* Get the reminder's time and date
* @return calendar
*/
public Calendar getReminderTimeAndDate()
{
return calendar;
}
public boolean isReminderFlag()
{
return reminderFlag;
}
public void setReminderFlag(boolean reminderFlag)
{
this.reminderFlag = reminderFlag;
}
}
对话框的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DateTimePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:padding="5dip" >
<LinearLayout
android:id="@+id/ViewSwitchButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip" >
<Button
android:id="@+id/button_SwitchToDate"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:text="Set date" />
<Button
android:id="@+id/button_SwitchToTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Set time" />
</LinearLayout>
<ViewSwitcher
android:id="@+id/viewSwitcher_DateTimePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ViewSwitchButtons" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<DatePicker
android:id="@+id/picker_DatePicker"
android:calendarViewShown="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<TimePicker
android:id="@+id/picker_TimePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
</LinearLayout>
</ViewSwitcher>
<LinearLayout
android:id="@+id/OkResetCancelButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/viewSwitcher_DateTimePicker"
android:layout_marginBottom="5dip"
android:orientation="horizontal" >
<Button
android:id="@+id/button_Ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok" />
<Button
android:id="@+id/button_Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset" />
<Button
android:id="@+id/button_Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
使用开关按钮的活动:
public class CreateNewTaskActivity extends Activity
{
public final static String EXTRA_DESCRIPTION = "il.ac.shenkar.remindme.DESCRIPTION";
private Calendar calendar;
private Context context;
private Switch switchReminder;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_task);
//getActionBar().setDisplayHomeAsUpEnabled(true);
calendar = Calendar.getInstance();
context = this;
switchReminder = (Switch) findViewById(R.id.switch_reminder);
switchReminder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
showDateTimePickerDialog(buttonView);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_description, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Calls when the user clicks the "Create" button.
* @param view
*/
public void createNewTask(View view)
{
// get the description from the EditText and check if it's valid
EditText editText = (EditText) findViewById(R.id.edit_description);
String description = editText.getText().toString();
if (description.length() == 0)
{
Toast.makeText(CreateNewTaskActivity.this, "You must fill in description!", Toast.LENGTH_LONG).show();
}
else
{
Task newTask = new Task(description, new GregorianCalendar().getTimeInMillis(), false);
TasksDAO tasksDao = TasksDAO.getInstance(this);
tasksDao.addNewTask(newTask);
finish();
}
}
public void backToMainActivity(View view)
{
finish();
}
/**
* Open's up the dialog in which the user sets the date end the time.
* In case the user cancelled the reminder via dialog, the switchReminder
* will be set to "OFF" again.
* @param view - which is the switchReminder
*/
public void showDateTimePickerDialog(CompoundButton view)
{
DateTimePickerDialog dialog = new DateTimePickerDialog(context);
if(dialog.isReminderFlag())
{
calendar = dialog.getReminderTimeAndDate();
view.setChecked(true);
}
else
{
view.setChecked(false);
}
}
}
答案 0 :(得分:0)
使DateTimePickerDialog对话框成为CreateNewTaskActivity类的类成员,当您决定显示该对话框时,将其实例化。所以基本上当对话框被取消时,根据任何活动的生命周期onResume()将被调用。在CreateNewTaskActivity类的onResume()方法中,首先检查dialog.isReminderFlag()是true还是false,然后检查n取消相应的取消!所以你的CreateNewTaskActivity类代码如下所示:
public class CreateNewTaskActivity extends Activity
{
public final static String EXTRA_DESCRIPTION = "il.ac.shenkar.remindme.DESCRIPTION";
private Calendar calendar;
private Context context;
private Switch switchReminder;
private DateTimePickerDialog dialog;
@Override
public void onResume(){
if(dialog != null){
if(dialog.isReminderFlag())
{
calendar = dialog.getReminderTimeAndDate();
view.setChecked(true);
}
else
{
view.setChecked(false);
}
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_task);
//getActionBar().setDisplayHomeAsUpEnabled(true);
calendar = Calendar.getInstance();
context = this;
switchReminder = (Switch) findViewById(R.id.switch_reminder);
switchReminder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
showDateTimePickerDialog(buttonView);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_description, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Calls when the user clicks the "Create" button.
* @param view
*/
public void createNewTask(View view)
{
// get the description from the EditText and check if it's valid
EditText editText = (EditText) findViewById(R.id.edit_description);
String description = editText.getText().toString();
if (description.length() == 0)
{
Toast.makeText(CreateNewTaskActivity.this, "You must fill in description!", Toast.LENGTH_LONG).show();
}
else
{
Task newTask = new Task(description, new GregorianCalendar().getTimeInMillis(), false);
TasksDAO tasksDao = TasksDAO.getInstance(this);
tasksDao.addNewTask(newTask);
finish();
}
}
public void backToMainActivity(View view)
{
finish();
}
/**
* Open's up the dialog in which the user sets the date end the time.
* In case the user cancelled the reminder via dialog, the switchReminder
* will be set to "OFF" again.
* @param view - which is the switchReminder
*/
public void showDateTimePickerDialog(CompoundButton view)
{
dialog = new DateTimePickerDialog(context);
if(dialog.isReminderFlag())
{
calendar = dialog.getReminderTimeAndDate();
view.setChecked(true);
}
else
{
view.setChecked(false);
}
}
接口监听器:
public interface IDialogListener {
public void changeSwitchState(boolean state);
}
类YourMainActivity实现了IDialogListener {
//implement the interface method
public void changeSwitchState(boolean state){
//surround the UI change responsible codes like the one below inside UI thread runnable , activity.runOnUiThread();
switch.setChecked(state);
// if required invaliadate();//
}
}
答案 1 :(得分:0)
使用onClickListener拦截任何触摸并从对话框中手动切换开关。