0

我有一个打开自定义对话框的开关。在 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);
    }
}

}

4

2 回答 2

0

使 DateTimePickerDialog 对话框成为 CreateNewTaskActivity 类的类成员,当您决定显示对话框时,将其实例化。所以基本上当对话框被取消时,根据任何活动的生命周期 onResume() 将被调用。在 CreateNewTaskActivity 类的 onResume() 方法中,首先检查 dialog.isReminderFlag() 是真还是假,然后相应地检查 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();//
}
}
于 2012-12-26T15:36:41.773 回答
0

使用 onClickListener 拦截任何触摸并从对话框中手动切换开关。

于 2012-12-26T16:17:59.437 回答