【问题标题】:android DatePickerDialog display only one buttonandroid DatePickerDialog 只显示一个按钮
【发布时间】:2013-08-23 17:06:20
【问题描述】:

我知道DatePickerDialog 存在很多问题,正如本文Jelly Bean DatePickerDialog --- is there a way to cancel? 中所解释的那样,但我的问题是我只想 显示确认按钮而不是两个按钮。我在DialogFragment 中使用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);
}
}

【问题讨论】:

  • 你的意思是删除Cancel按钮?
  • 默认系统日期选择器将有两个按钮。如果您只想拥有一个按钮,那么您可以使用自定义日期选择器。

标签: android datepickerdialog


【解决方案1】:

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

}

【讨论】:

  • 也适用于 TimePickerDialog
  • 像魅力一样工作。谢谢你。看来你甚至可以做到dialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, null, (OnClickListener) null);
【解决方案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;
        }
    }
}

在这里你只设置了一个按钮,“确定”按钮!

【讨论】:

  • 没有更简单的方法吗?事实上,我使用 DialogFragment 来显示两个对话框(第一个 -> 日期,然后 -> 时间),使用这个解决方案我必须更改很多东西。此外,我有一些限制,例如从五到五步来设置分钟。
【解决方案3】:

你可以在 Kotlin 中做到这一点:

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

其中 dpd 是您的 DatePickerDialog 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 2013-03-27
    • 2021-09-14
    • 2021-03-27
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多