Sav*_*nar 11 time android widget picker timepicker
我想在我的Android应用程序中添加新按钮,这是清除按钮,日期选择器小部件和时间选择器小部件.默认情况下,这些小部件有两个按钮,设置和取消.如何在这两个按钮上再添加一个按钮
可能吗?如果是的话,你能举个例子吗?
谢谢
und*_*ned 37
只需添加一个中性按钮.
DatePickerDialog dialog = new DatePickerDialog(context, 0, callback, year, month, day);
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Name", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Your code
}
});
Run Code Online (Sandbox Code Playgroud)
只需创建这个课程!
import android.app.DatePickerDialog;
import android.content.Context;
public class DatePickerWithNeutral extends DatePickerDialog {
public DatePickerWithNeutral(Context context, OnDateSetListener callBack,
int year, int monthOfYear, int dayOfMonth) {
super(context, 0, callBack, year, monthOfYear, dayOfMonth);
setButton(BUTTON_POSITIVE, ("Ok"), this);
setButton(BUTTON_NEUTRAL, ("Something"), this); // ADD THIS
setButton(BUTTON_NEGATIVE, ("Cancel"), this);
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用它来添加功能!
date.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Neutral Button Clicked!",
Toast.LENGTH_LONG).show();
}
});
Run Code Online (Sandbox Code Playgroud)
看起来像这样

请享用 :)
这就是我在我的应用程序中实现“清除”按钮的方式。当用户单击清除时,年/月/日值全部为 0。您可以在您的应用程序中使用 onDateSet() 以这种方式设置按钮和清除按钮。
我引用了 Android 源代码 (\frameworks\base\core\java\android\app\DatePickerDialog.java)。
我也使用了esilver的帮助。
public class DatePickerDialogPlus extends DatePickerDialog {
private final DatePicker mDatePicker;
private final OnDateSetListener mCallBack;
/**
* @param context The context the dialog is to run in.
* @param callBack How the parent is notified that the date is set.
* @param year The initial year of the dialog.
* @param monthOfYear The initial month of the dialog.
* @param dayOfMonth The initial day of the dialog.
*/
public DatePickerDialogPlus(Context context, OnDateSetListener callBack,
int year, int monthOfYear, int dayOfMonth) {
super(context, 0, callBack, year, monthOfYear, dayOfMonth);
mCallBack = callBack;
Context themeContext = getContext();
setButton(BUTTON_POSITIVE,
themeContext.getText(R.string.datePicker_setButton), this);
setButton(BUTTON_NEUTRAL,
themeContext.getText(R.string.datePicker_clearButton), this);
setButton(BUTTON_NEGATIVE,
themeContext.getText(R.string.datePicker_cancelButton), null);
setIcon(0);
setTitle(R.string.datePicker_title);
LayoutInflater inflater = (LayoutInflater)
themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.date_picker_dialog, null);
setView(view);
mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
mDatePicker.init(year, monthOfYear, dayOfMonth, this);
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (mCallBack != null) {
if (which == BUTTON_POSITIVE) {
mDatePicker.clearFocus();
mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
} else if (which == BUTTON_NEUTRAL) {
mDatePicker.clearFocus();
mCallBack.onDateSet(mDatePicker, 0, 0, 0);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)