android - 更改 AlertDialog.Builder 中按钮的颜色

标签 android android-studio-2.2

给定一个函数作为

private void showInputDialog() {
    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    final EditText input = new EditText(getActivity());
    input.setSingleLine();
    FrameLayout container = new FrameLayout(getActivity());
    FrameLayout.LayoutParams params = new  FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.leftMargin= convertDpToPx(25);                                       //remember to scale correctly
    params.rightMargin= convertDpToPx(30);
    input.setLayoutParams(params);
    container.addView(input);
    alert.setTitle("Change City");
    alert.setMessage("Hey there, could not find the city you wanted. Please enter a new one:\n");
    alert.setView(container);
    alert.setPositiveButton("Go", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            changeCity(input.getText().toString());
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alert.show();
}

现在,当我在我的应用程序中获得此 AlertDialog.Builder 时,按钮颜色为绿色(Android 5 的默认颜色),但 EditText 颜色为粉红色(R.color.coloraccent)。如何将按钮的颜色更改为粉红色?

任何帮助将不胜感激

最佳答案

这样试试

alertDialog.show(); 

仅在调用 .show() 之后。 试试这个 fragment

//for negative side button
    alertDialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(neededColor); 
//for positive side button
    alertDialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(neededColor);

关于android - 更改 AlertDialog.Builder 中按钮的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930505/

相关文章:

Android 设备是否经过硬件加密?以编程方式

jenkins - 使用带有命令行 SDK 的 Android Studio 2.2 CMake 支持

Android Studio 无法撤消

java - 在Android设备上选择不同的语言选择导致日期解析错误

android-ndk - 使用 Android Studio 的 gradle build 将 tess-two 构建到项目中

android - 在 ImageView 上添加效果

java - "Choose an account"点击取消时表格不消失

android - 如何使 ConstraintView 内部的 WebView 有边界?

android - 在 android ScrollView 上显示更大的标尺

android studio 2.2 布局编辑器刷新按钮