Android Studio 如何更改对话框片段中按钮颜色

fumotvh3  于 2022-12-23  发布在  Android
关注(0)|答案(1)|浏览(188)

我有这个类java:

public class DatePickerFragment extends DialogFragment {
    private Calendar date;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        DatePicker datePicker = new DatePicker(getActivity());
        date = Calendar.getInstance();

        return new AlertDialog.Builder(getActivity())
                .setView(datePicker)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                date.set(Calendar.YEAR, datePicker.getYear());
                                date.set(Calendar.MONDAY, datePicker.getMonth());
                                date.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());

                                ((SigninActivity)getActivity()).doPositiveClick(date);
                            }
                        }
                        
                )

                .setNegativeButton("Back", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dismiss();
                    }
                })

                .create()
                ;
        
    }

我想更改两个按钮“ok”和“back”的颜色。颜色是themes.xml文件中的颜色,colorPrimary,但我不知道在哪里更改此设置。
我不想更改colorPrimary,因为我只需要自定义这两个按钮。
下面是我调用datepicker来设置日期的地方:

data.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        new DatePickerFragment().show(getSupportFragmentManager(), DatePickerFragment.TAG);

    }
});
clj7thdc

clj7thdc1#

您可以通过在onStart()中执行以下操作来更改按钮颜色:

public void onStart() {
    super.onStart();
    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.GREEN);
    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
}

相关问题