自定义日期选择器按钮。 Android 中的自定义组件
我正在尝试创建一个自定义日期选择器按钮组件。该按钮显示日期,单击时会打开日期选择器对话框。我已经使用 Android 开发文档/教程实现了这个方法,但是我的代码中有两个,我认为最好创建一个名为 DateButton 的自定义类,它可以稍微清理我的代码。因此,每当我需要类似的按钮时,我都可以在 XML 中创建一个声明 DateButton。无论如何,这是我第一次创建自定义视图,每当我尝试将自定义按钮添加到布局中时,我都需要一些帮助,但我收到错误:
error! ClassNotFoundException: android.app.DatePickerDialog$OnDateSetListener
这是我希望按钮查看的内容(非常简单)
单击它时,将弹出内置的 android 日期选择器对话框:
这是我当前此自定义日期按钮的代码:
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
public class DateButton extends Button implements OnClickListener, OnDateSetListener {
private static final int DATE_DIALOG_ID = 0;
private int mYear;
private int mMonth;
private int mDay;
private OnDateSetListener mDateSetListener;
public DateButton(Context context) {
super(context);
}
public DateButton(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public DateButton(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
private DatePickerDialog showDialog(int dateDialogId) {
return new DatePickerDialog(getContext(),
mDateSetListener,
mYear, mMonth, mDay);
}
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
private void updateDisplay() {
this.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(pad(mMonth + 1)).append("/")
.append(pad(mDay)).append("/")
.append(mYear).append(" "));
}
//if single digit append "0" to the number
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
编辑:我仍然遇到麻烦,任何人都有任何示例与此类似的东西? 感谢您的帮助!
I am trying to create a custom date picker button component. The button displays the date and when clicked it opens the date picker dialog box. I have implemented this method using the Android Dev Docs/Tutorial however I have 2 of them in my code and I figured it would be better to create a custom class called DateButton which would clean up my code a bit. So whenever I need a similar button I can create a declare a DateButton in my XML. Anyways this is my first time creating a custom view and I need some help whenever I try to add my custom button to my layout I get an error:
error! ClassNotFoundException: android.app.DatePickerDialog$OnDateSetListener
Here is what I want the button to look at (very simple)
and when it is clicked it will pop up the built in android datepicker dialog:
Here is my current code for this custom datebutton:
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
public class DateButton extends Button implements OnClickListener, OnDateSetListener {
private static final int DATE_DIALOG_ID = 0;
private int mYear;
private int mMonth;
private int mDay;
private OnDateSetListener mDateSetListener;
public DateButton(Context context) {
super(context);
}
public DateButton(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public DateButton(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
private DatePickerDialog showDialog(int dateDialogId) {
return new DatePickerDialog(getContext(),
mDateSetListener,
mYear, mMonth, mDay);
}
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
private void updateDisplay() {
this.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(pad(mMonth + 1)).append("/")
.append(pad(mDay)).append("/")
.append(mYear).append(" "));
}
//if single digit append "0" to the number
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
EDIT: I'm still running into trouble anyone have any examples of something similar to this?
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
showDialog
方法更改为:同时添加
mYear、mMonth、mDay
的初始化代码。Try to change
showDialog
method to:Also add initialize code for
mYear, mMonth, mDay
.