更改日期选取器对话框中其中一个按钮的文本颜色?



我有一个主题为这个的日期选择器对话框

<style name="DatePickerTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/that_blue_color</item>
</style>

我制作了这个自定义主题,因为我想更改日期PickerDialog的背景颜色。我也设法更改了背景的颜色,选择器圆圈和按钮的文本颜色。但是我现在想更改CANCEL按钮的文本颜色,并保持OK按钮的文本颜色不变。我该怎么做?

您可以从Dialog获取Button并使用getButton()修改其属性。请参阅下面的示例。调用后获取按钮.show()其他明智的做法,它会给出一个null

final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int  mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(ConstarintsActivity.this,
(view, year, monthOfYear, dayOfMonth) -> {

}, mYear, mMonth, mDay);
datePickerDialog.show();
datePickerDialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(Color.GREEN);

您可以为按钮制作不同的样式CANCEL如下所示。

<style name="DatePickerTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:buttonBarNegativeButtonStyle">@style/DatePickerTheme.ButtonBarNegativeButtonStyle</item>
</style>
<style name="DatePickerTheme.ButtonBarNegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@android:color/holo_red_light</item>
</style>
dialog.setOnShowListener(dialog -> {
int positiveColor = ContextCompat.getColor(view.getContext(), R.color.default_blue_color);
int negativeColor = ContextCompat.getColor(view.getContext(), R.color.font_hint);
dialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setTextColor(positiveColor);
dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(negativeColor);
});
dialog.show();

我在对话框的showListener中设置按钮颜色。

我设法这样做

datePickerDialog.show();
datePickerDialog.
getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(Color.G
RAY);

只需为CANCEL按钮制作不同的样式,并将该样式放入alertdialog的父主题中,如下所示:

<style name="DatePickerTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/that_blue_color</item>
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#f00</item>
</style>

只需在您的样式中添加以下内容.xml

<style name="mdtp_ActionButton.Text" parent="Widget.MaterialComponents.Button.TextButton.Dialog"/>

这是我的解决方案。

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- ... -->
<item name="android:datePickerDialogTheme">@style/SpinnerDatePickerStyle</item>
</style>
<style name="SpinnerDatePickerStyle" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">@style/myDatePickerStyle</item>
<item name="android:buttonBarPositiveButtonStyle">@style/DatePickerButtonStyle</item>
<item name="android:buttonBarNegativeButtonStyle">@style/DatePickerButtonStyle</item>
</style>
<style name="myDatePickerStyle" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
<style name="DatePickerButtonStyle" parent="android:Widget.Material.Button.Borderless">
<item name="android:textColor">@color/black</item>
</style>

这不是buttonBarPositiveButtonStyle,而是android:buttonBarPositiveButtonStyleandroid:

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

在 AndroidManifest 中.xml文件在颗粒活动 android:theme="@style/DatePickerTheme" 中添加此行。

在 XML 文件中尝试此操作:

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Date"
/>

现在在 Java 文件中:

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Initialize a new date picker dialog fragment
DialogFragment dFragment = new DatePickerFragment();
// Show the date picker dialog fragment
dFragment.show(getFragmentManager(), "Date Picker");
}
});
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
/*
Create a DatePickerDialog using Theme.
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
int year, int monthOfYear, int dayOfMonth)
*/
// DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);
// Return the DatePickerDialog
return  dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day){
// Do something with the chosen date
}

我希望它能帮助你..!

edt_dob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Birthday_click = false;
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance((DatePickerDialog.OnDateSetListener) context,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setMaxDate(Calendar.getInstance());
dpd.show(getFragmentManager(), "Datepickerdialog");
dpd.dismissOnPause(true);
dpd.setAccentColor(000000);
dpd.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
edt_dob.setText(null);
edt_dob.setInputType(InputType.TYPE_NULL);
int day = dayOfMonth;
int yy = year;
int month = ++monthOfYear;
String str_day = day < 10 ? "0" + day : "" + day;
String str_month = month < 10 ? "0" + month : "" + month;
String date_select = yy + "-" + str_month + "-" + str_day;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
try {
date1 = format.parse(date_select);
} catch (ParseException e) {
e.printStackTrace();
}
String date = format.format(date1);
if (date.endsWith("01") && !date.endsWith("11"))
format = new SimpleDateFormat("MMMM d'st', yyyy");
else if (date.endsWith("02") && !date.endsWith("12"))
format = new SimpleDateFormat("MMMM d'nd', yyyy");
else if (date.endsWith("03") && !date.endsWith("13"))
format = new SimpleDateFormat("MMMM d'rd', yyyy");
else
format = new SimpleDateFormat("MMMM d'th', yyyy");
String yourDate = format.format(date1);
edt_dob.setText(yourDate);
}
});
}
});

最新更新