Java 从日期选择器更改日期时显示文本
请看一下下面的代码 活动\u main.xmlJava 从日期选择器更改日期时显示文本,java,android,datepicker,Java,Android,Datepicker,请看一下下面的代码 活动\u main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orien
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/heading"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<DatePicker
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
在这里,我想在日期更改时显示“Toast”消息。我在“DatePickerMe”内部类下尝试过。但是,我不知道如何将这个“OnDateSetListener”注册到日期选择器(我假设OnDateSetListener是正确的接口)。无论何时更改日期,请帮助我显示文本
感谢您在调用init方法时注册了侦听器
dp.init( year, month, day, new DatePickerMe() );
我注意到一些事情:
- 您需要使用设置OnDateChangedListener李>
- 另外,您必须在祝酒词上调用
,否则它将永远不会显示李>show()
- 最后,您不需要扩展OnDateChangeListener类,您可以像下面那样使用它
OnDateChangedListener listener = new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Show your Toast by calling show()!
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_LONG).show();
}
};
DatePicker dp = (DatePicker)findViewById(R.id.date);
// Use Calendar to set the date to now
Calendar calendar = Calendar.getInstance();
dp.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), listener);
谢谢你的帮助!我真的很感激!谢谢你的回复……我真的很感激:)+1
OnDateChangedListener listener = new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Show your Toast by calling show()!
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_LONG).show();
}
};
DatePicker dp = (DatePicker)findViewById(R.id.date);
// Use Calendar to set the date to now
Calendar calendar = Calendar.getInstance();
dp.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), listener);