【问题标题】:How to finish activity when date picker dialog on cancel button is clicked单击取消按钮上的日期选择器对话框时如何完成活动
【发布时间】:2016-01-30 16:32:49
【问题描述】:

我已经在我的活动中实现了日期选择器对话框,它工作正常。当我单击取消时,选择器被关闭,因此按钮起作用。但是,我想捕获取消按钮事件并向其添加更多代码以完成活动,但我似乎无法处理。 这是我的代码:

public class DatePickerActivity extends Activity {
    static final int DATE_PICKER_ID = 1111;
    private int year;
    private int month;
    private int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker);
        // Get current date by calender

        final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);

        showDialog(DATE_PICKER_ID);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_PICKER_ID:

                // open datepicker dialog.
                // set date picker for current date
                // add pickerListener listner to date picker
                return new DatePickerDialog(this, pickerListener, year, month,day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {

            year  = selectedYear;
            month = selectedMonth;
            day   = selectedDay;

            // Show selected date
            Log.d("Date selected", String.valueOf(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" ")));

            Intent intent = new Intent();
            intent.putExtra("year", String.valueOf(new StringBuilder().append(year )));
            intent.putExtra("month", String.valueOf(new StringBuilder().append(month + 1)));
            intent.putExtra("day",  String.valueOf(new StringBuilder().append(day)));
            setResult(RESULT_OK, intent);
            finish();

        }
    };
}

我尝试的是添加这个 onclick 方法,例如:

 public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

        if(which==Dialog.BUTTON_NEGATIVE)
        {
            Log.i("dialog click", "dialog negative button clicked");
            dialog.dismiss();
        }

    }

但这不起作用。有什么建议吗?

【问题讨论】:

  • 感谢您的回复@DanielNugent。我的意思是使用我上面的代码,您的建议需要对整个代码采用不同的方法。
  • 看起来不会有太大变化,您基本上可以将已接受答案中的代码添加到您的 onCreateDialog() 代码中。而不是return new DatePickerDialog(),您需要一个 DatePickerDialog 参考,例如DatePickerDialog ref = new DatePickerDialog(),在引用上调用setButton(),然后返回。
  • 效果很好!!我实现了你的解决方案

标签: android android-datepicker


【解决方案1】:

为 DatePickerDialog 创建一个类并从onCreate() 调用它,如下所示:

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);

        // Return the DatePickerDialog
        return  dpd;
    }

    public void onDateSet(DatePicker view, int year, int month, int day){
        // Your code here
    }

    // Set a Listener for Dialog Cancel button click event
    /*
        onCancel(DialogInterface dialog)
            This method will be invoked when the dialog is canceled.
     */
    public void onCancel(DialogInterface dialog){
        // Send a message to confirm cancel button click
        Toast.makeText(getActivity(),"Date Picker Canceled.", Toast.LENGTH_SHORT).show();
    }
}

onCreate()拨打电话

 @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker);
        DialogFragment dFragment = new DatePickerFragment();
        // Show the date picker dialog fragment
        dFragment.show(getFragmentManager(), "Date Picker");
   }

【讨论】:

  • API 10 及以下版本是否支持此功能?
  • 尚未检查 API 10,但我想它支持。
  • 看起来很干净,我喜欢。感谢您的解决方案!
【解决方案2】:

你可以试试这个代码

 @Override
  protected Dialog onCreateDialog(int id) {
    switch (id) {
      case DATE_PICKER_ID:

        // open datepicker dialog.
        // set date picker for current date
        // add pickerListener listner to date picker
        DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month,day);


        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Text", new DialogInterface
            .OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_NEGATIVE) {
              // Do Stuff
             Log.i("dialog click", "dialog negative button clicked");
            dialog.dismiss();
            }
          }
        });
        return dialog;
    }
    return null;
  }

【讨论】:

  • @SteveKamau:很高兴..:)
猜你喜欢
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多