2013-08-21 25 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