2013-08-21 33 views
2

我知道在這個崗位Jelly Bean DatePickerDialog --- is there a way to cancel?有很多的問題,如DatePickerDialog解釋,但我的問題是,我想顯示的確認按鈕,而不是兩個按鈕。我在DialogFragment使用DatePickerDialog這樣的:安卓DatePickerDialog顯示只有一個按鈕

public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener { 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the current date as the default date in the picker 
    final Calendar c = Calendar.getInstance(); 
    int year = c.get(Calendar.YEAR); 
    int month = c.get(Calendar.MONTH); 
    int day = c.get(Calendar.DAY_OF_MONTH); 

    // set up mindate 
    minYear = year; 
    minMonth = month; 
    minDay = day; 

    // Create a new custom instance of DatePickerDialog and return it 
    return new CustomDatePickerDialog(getActivity(), this, year, month, day); 
} 
} 
+0

你的意思是刪除'Cancel'按鈕workaraound? –

+0

默認的系統日期選擇器將有兩個按鈕。如果你想只有一個按鈕,那麼你可以使用自定義日期選擇器。 – Hariharan

+0

@StefanoMunarini是的 – mrroboaat

回答

10

我終於找到了一個更簡單的解決方案。這裏是我的CustomDatePickerDialog延伸DatePickerDialog

public CustomDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear,int dayOfMonth) { 
    super(context, callBack, year, monthOfYear, dayOfMonth); 
    this.setButton(DatePickerDialog.BUTTON_POSITIVE, "OK",this); 
    this.setButton(DatePickerDialog.BUTTON_NEGATIVE, "",this); // hide cancel button 

} 
+0

也適用於TimePickerDialog –

+0

工程就像一個魅力。謝謝。看起來你甚至可以做'dialog.setButton(DatePickerDialog.BUTTON_NEGATIVE,null,(OnClickListener)null);'。 – lupz

2

您可以使用自定義XML這樣的:

datepickerdialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 


<DatePicker 
     android:id="@+id/dp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:calendarViewShown="false"/>  //delete this line if you want to show calendar too 


</RelativeLayout> 

然後偵聽器添加到按鈕(或其他)和膨脹這樣的佈局如下:

yourclass.java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.aggiungiesame); 

    Button button = (Button) findViewById(R.id.your_id_button); 
    button.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
     case R.id.button: {     
      LayoutInflater inflater = getLayoutInflater(); 
      View customView = inflater.inflate(R.layout.datepickerdialog, null); 

      DatePicker dp = (DatePicker) customView.findViewById(R.id.dp);    

      ...OTHER CODE HERE 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setView(customView); 
      builder.setTitle("Select date:"); 

      builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        ...YOUR CODE HERE... 
        dialog.dismiss(); 
       } 
      }); 
      builder.create().show(); 
      break; 
     } 
    } 
} 

在這裏,您只設置了一個按鈕,'OK'按鈕!

+0

有沒有更簡單的方法?實際上,我使用DialogFragment來顯示兩個對話框(第一個 - >日期,然後 - >時間),並且使用這個解決方案我必須改變很多東西。此外,我還有一些限制,比如從五點到五點設置分鐘。 – mrroboaat