<TextView
android:id="@+id/tv_arrival"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/arrivaldate"/>
<Button
android:id="@+id/btn_arrival"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Set"/>
我想显示datepicker弹出窗口。我找到了一些例子,但我没有得到正确的答案。我有一个按钮,我希望当我点击按钮时,弹出日期选择器对话框,设置日期后,日期应显示在一个。 PLease为我提供了示例代码或良好的链接。
btn_arrival.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog setDate = new Dialog(MainActivity.this);
setDate.requestWindowFeature(Window.FEATURE_NO_TITLE);
setDate.setContentView(R.layout.dialog_setdate);
setDate.show();
DatePicker date = (DatePicker) setDate.findViewById(R.id.datePicker);
date.setMinDate(System.currentTimeMillis() - 1000);
Calendar calender = Calendar.getInstance();
DatePicker.OnDateChangedListener onDateChangedListener = new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int years, int monthOfYear, int dayOfMonth) {
day = dayOfMonth;
year = years;
month = monthOfYear;
setDate.dismiss();
txt_arrival.setText("Arrival Date on: " + day + "." + (month + 1) + "." + year);
tv_departure.setText("Departure Date on: " + (day + 1) + "." + (month + 1) + "." + year + "");
}
};
day = calender.get(Calendar.DAY_OF_MONTH);
month = calender.get(Calendar.MONTH);
year = calender.get(Calendar.YEAR);
date.init(year, month, day, onDateChangedListener);
}
});
答案 0 :(得分:2)
这是示例代码 像你这样在你的视图上设置onclicklistener
Calendar dateSelected = Calendar.getInstance();
private DatePickerDialog datePickerDialog;
dateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setDateTimeField() ;
}
});
private void setDateTimeField() {
Calendar newCalendar = dateSelected;
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateSelected.set(year, monthOfYear, dayOfMonth, 0, 0);
dateEditText.setText(dateFormatter.format(dateSelected.getTime()));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
dateEditText.setText(dateFormatter.format(dateSelected.getTime()));
}
或者您可以点击此链接
答案 1 :(得分:0)
这是一个针对DatePicker的AlertDialog片段的解决方法,它在日期中将日期放入TextView。
DatePicker的xml
您可以省略CardView根布局并使用LinearLayout,RelativeLayout等任何内容。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
card_view:cardElevation="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/date_time_title"/>
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="0dp"
android:calendarViewShown="true"
android:spinnersShown="false"
android:layout_weight="1"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/date_time_not_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_gravity="start"/>
<Button
android:id="@+id/date_time_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"
android:layout_gravity="end"/>
</FrameLayout>
</LinearLayout>
活动/片段的java文件
[...] //other code
btn_arrival.setOnClickListener( this );
// Activity/Fragment implements onClick()
@Override
public void onClick( final View v )
{
switch ( v.getId( ) )
{
case R.id.btn_arrival: createDatePicker( tv_arrival); break;
}
}
private void createDatePicker( final TextView tv_arrival )
{
final View dialogView = View.inflate( getActivity( ), R.layout.date_picker, null );
final AlertDialog alertDialog = new AlertDialog.Builder( getActivity( ) ).create( );
TextView tv = ( TextView ) dialogView.findViewById( R.id.date_time_title );
tv.setText( ); // set title here
dialogView.findViewById( R.id.date_time_set ).setOnClickListener( new View.OnClickListener( )
{
@Override
public void onClick( View view )
{
setDatePicker( dialogView, tv_arrival );
alertDialog.dismiss( );
}
} );
dialogView.findViewById( R.id.date_time_not_set ).setOnClickListener( new View.OnClickListener( )
{
@Override
public void onClick( View v )
{
alertDialog.dismiss( );
}
} );
alertDialog.setView( dialogView );
alertDialog.show( );
}
private void setDatePicker ( View dialogView, TextView tv_arrival )
{
DatePicker datePicker = ( DatePicker ) dialogView.findViewById( R.id.date_picker );
Calendar calendar;
SimpleDateFormat sdf = new SimpleDateFormat( "dd-MM-yyyy", Locale.ENGLISH );
calendar = new GregorianCalendar( datePicker.getYear( ), datePicker.getMonth( ), datePicker.getDayOfMonth( ) );
tv_arrival.setText( sdf.format( calendar.getTime( ) ) );
}