3

DatePickerDialog我知道在这篇文章 Jelly Bean DatePickerDialog 中解释了很多问题——有没有办法取消?但我的问题是我只想显示确认按钮而不是两个按钮。我像这样使用DatePickerDialogin DialogFragment

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);
}
}
4

3 回答 3

11

我终于找到了一个更简单的解决方案。这是我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

}
于 2013-10-09T14:33:08.733 回答
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;
        }
    }
}

在这里您只需设置一个按钮,即“确定”按钮!

于 2013-08-21T11:43:29.390 回答
1

您可以在 Kotlin 中执行此操作:

    dpd.setButton(DatePickerDialog.BUTTON_NEGATIVE, null, null as DialogInterface.OnClickListener? )

其中 dpd 是您的 DatePickerDialog 实例。

于 2020-03-02T17:45:20.120 回答