gpt4 book ai didi

android - android 中的日期选择器

转载 作者:行者123 更新时间:2023-11-30 04:58:08 28 4
gpt4 key购买 nike

我想要可以在其中选择年、月和日期的日期选择器。像这样,

enter image description here

但我得到这样的日期选择器,

enter image description here

我想选择年份和月份,但在上面的日期选择器中我只能选择月份这是我的代码:

DatePickerDialog.OnDateSetListener mDateListener;

edBirthday.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog = new DatePickerDialog(
RegisterActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateListener,
year,month,day
);
dialog.getWindow().setBackgroundDrawable( new ColorDrawable( Color.TRANSPARENT ) );
dialog.show();
}
} );

mDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker ,int year ,int month ,int day) {
month = month + 1;

Log.d( "onDateSet" , month + "/" + day + "/" + year );
edBirthday.setText( new StringBuilder().append( day ).append( "-" )
.append( month ).append( "-" ).append( year ) );
}
};

最佳答案

是的,这可以使用自定义对话框,

创建 lay_calander.xml

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false"
android:datePickerMode="spinner" />

为您的自定义对话框创建 Java 类,

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;

import java.util.Calendar;

public class MyDatePickerDialog extends AlertDialog implements DialogInterface.OnClickListener, DatePicker.OnDateChangedListener {

private DatePickerDialog.OnDateSetListener mDateSetListener;
private DatePicker mDatePicker;

protected MyDatePickerDialog(@NonNull Context context) {
super(context);
init();
}

protected MyDatePickerDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
init();
}

protected MyDatePickerDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
init();
}


private void init() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.lay_calander, null);
mDatePicker = view.findViewById(R.id.datePicker);
setView(view);
}

public void showDatePicker(DatePickerDialog.OnDateSetListener listener, Calendar defaultDate) {

setButton(BUTTON_POSITIVE, getContext().getString(android.R.string.ok), this);
setButton(BUTTON_NEGATIVE, getContext().getString(android.R.string.cancel), this);

mDateSetListener = listener;

if (defaultDate == null) {
defaultDate = Calendar.getInstance();

}
int year = defaultDate.get(Calendar.YEAR);
int monthOfYear = defaultDate.get(Calendar.MONTH);
int dayOfMonth = defaultDate.get(Calendar.DAY_OF_MONTH);
mDatePicker.init(year, monthOfYear, dayOfMonth, this);

show();

}


@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
switch (which) {
case BUTTON_POSITIVE:
if (mDateSetListener != null) {
// Clearing focus forces the dialog to commit any pending
// changes, e.g. typed text in a NumberPicker.
mDatePicker.clearFocus();
if (mDateSetListener != null) {
mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
}
}
break;
case BUTTON_NEGATIVE:
cancel();
break;
}
}

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDatePicker.init(year, monthOfYear, dayOfMonth, this);
}
}

从您的 Activity 中调用对话框,

MyDatePickerDialog dialog = new MyDatePickerDialog(this);
dialog.setTitle("Set Date");
dialog.showDatePicker(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//Date select callback
}
}, Calendar.getInstance());

你的输出看起来像, enter image description here

确保你的 AppTheme 应该是 Extend AppCompat, list - android:theme="@style/AppTheme"

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

关于android - android 中的日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58776824/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com