2013-02-22 38 views
2

我正在使用以下方法彈出對話框來選擇日期。如何在DatePickerDialog.OnDateSetListener中獲取OnClick?

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

      public void onDateSet(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) { 
       dobYear = year; 
       dobMonth = monthOfYear; 
       dobDay = dayOfMonth; 
       if(isEighteenYearOld()){ 
        //display the current date 
        dateDisplay(); 
       } else{ 
        Toast.makeText(mContext, "You must be 18 year old", Toast.LENGTH_SHORT).show(); 
       } 
      } 

     }; 

我知道onDateSet我們可以得到選定的日期。但是我想要的是如果選擇的日期比18歲小,我需要提醒用戶。我已經嘗試了上面的代碼,但它關閉了對話框並返回到活動。

我想留在對話框中,直到用戶選擇18歲的日期。我不知道如何獲得對話框中的onclick事件嗎?

回答

6

日期選取器對話框中你使用是棄用因此,我建議你不要使用..

您可以通過兩種方式實現的日期選擇器(據我所知) 1.使用DialogFragment 2.使用AlertDialog

第一:

public class MainActivity extends FragmentActivity { 
    EditText text; 
    Button b; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     b=(Button)findViewById(R.id.button1); 
     text=(EditText)findViewById(R.id.editText1); 
     b.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       DateDialogFragment datepicker=new DateDialogFragment(); 
       datepicker.show(getSupportFragmentManager(), "showDate"); 
      } 
     }); 
    } 

    public class DateDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ 

     public DateDialogFragment() 
     { 
     } 
     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      Calendar cal=Calendar.getInstance(); 
      int year=cal.get(Calendar.YEAR); 
      int month=cal.get(Calendar.MONTH); 
      int day=cal.get(Calendar.DAY_OF_MONTH); 
      return new DatePickerDialog(getActivity(), this, year, month, day); 
     } 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      showSetDate(year,monthOfYear,dayOfMonth); 
     } 

     } 

    public void showSetDate(int year,int month,int day) { 
    text.setText(year+"/+"+month+"/"+day); 
    } 
} 

CHEC k這個樣板,並在您的活動中執行相同的操作。

使用警報對話框: 使用第二個很簡單

在RES /佈局文件夾爲您詳細解答Tomik創建佈局,並放置在DatePicker的佈局

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view = (View) inflater.inflate(R.layout.yourlayout, null); 
DatePicker picker=(DatePicker)view.findViewById(R.id.datepicker); 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
builder.setView(view). 
     builder.setMessage(R.string.dialog_fire_missiles) 
       .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // FIRE ZE MISSILES! 
        } 
       }) 
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
+0

感謝您的回答。正如你在第一種方法中所說的,當對話框出現時你如何處理onClick? – GoCrazy 2013-02-22 18:58:49

+0

沒有必要單獨處理它與兩個按鈕設置和取消,並且當你設置日期它將在onDateSet處理Android – Pragnani 2013-02-22 19:04:14

+0

處理是的,我知道這一點。但是我想要的是我必須在將它傳入onDataSet之前進行驗證。正如我在我的問題中解釋的。 – GoCrazy 2013-02-22 19:16:32

1

如果您不想使用此類對話框的自定義實現,則必須繼承DatePickerDialog以實現此類行爲。您不能阻止對話框僅以DatePickerDialog.OnDateSetListener關閉。

不幸的是,對話框的實現隨不同的API級別而變化,所以用子類化來獲得所需的行爲並不是那麼簡單。你需要添加一些黑客來使其可靠工作。

我創建了一個示例實現,阻止對話框關閉,除非設置了適當的日期(或取消或返回按鈕被擊中)。調整它以向用戶顯示警報,最好的地方是onClick()方法中的else分支。

class CheckingDatePickerDialog extends DatePickerDialog { 

    private int year; 
    private boolean cancel = false; 
    private boolean isCancelable = true; 

    CheckingDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { 
     super(context, callBack, year, monthOfYear, dayOfMonth); 
     this.year = year; 
    } 

    CheckingDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { 
     super(context, theme, callBack, year, monthOfYear, dayOfMonth); 
     this.year = year; 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // allow closing the dialog with cancel button 
     Button btn = getButton(BUTTON_NEGATIVE); 
     if (btn != null) { 
      btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        cancel = true; 
        dismiss(); 
       } 
      }); 
     } 
    } 

    @Override 
    public void setCancelable(boolean flag) { 
     isCancelable = false; 
     super.setCancelable(flag); 
    } 

    @Override 
    public void onBackPressed() { 
     // allow closing the dialog with back button if the dialog is cancelable 
     cancel = isCancelable; 
     super.onBackPressed(); 
    } 

    private boolean isOldEnough() { 
     // test if the date is allowed 
     return year <= 1994; 
    } 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      // necessary for some Honeycomb devices 
      DatePicker dp = getDatePicker(); 
      this.year = dp.getYear(); 
     } 

     if (isOldEnough()) { 
      // OnDateSetListener is called in super.onClick() 
      super.onClick(dialog, which); 
     } else { 
      // place your alert here 
     } 
    } 

    @Override 
    public void onDateChanged(DatePicker view, int year, int month, int day) { 
     // on some Honeycomb devices called only with the first change 
     // necessary for devices running Android 2.x 
     this.year = year; 
     super.onDateChanged(view, year, month, day); 
    } 

    @Override 
    public void dismiss() { 
     if (cancel || isOldEnough()) { 
      // do not allow the dialog to be dismissed unless a cancel or back button was clicked 
      super.dismiss(); 
     } 
    } 
}; 
+0

感謝。我會嘗試+1 – GoCrazy 2013-02-22 19:58:16

相關問題