android DatePickerDialog display only one button

久未见 提交于 2020-03-22 07:19:11

问题


I know there are a lot of problems with DatePickerDialog as explain at this post Jelly Bean DatePickerDialog --- is there a way to cancel? but my problem is that I want to only display the confirmation button instead of two buttons. I use a DatePickerDialog in DialogFragment like this:

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

回答1:


I've finally found an easier solution. Here is the workaraound in my CustomDatePickerDialog which extends 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

}



回答2:


You can use a custom XML like this:

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>

Then add a listener to a button (or whatever) and inflate this layout like this:

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

Here you set just a button, the 'OK' button!




回答3:


You can do this in Kotlin:

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

Where dpd is your DatePickerDialog instance.



来源:https://stackoverflow.com/questions/18355524/android-datepickerdialog-display-only-one-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!