gpt4 book ai didi

java - 如何更改 AlertDialog View 背景颜色?

转载 作者:行者123 更新时间:2023-11-29 03:26:32 25 4
gpt4 key购买 nike

我想知道如何更改警报对话框中的灰色?我试过了:

layout.setBackgroundResource(R.color.Aqua);

它没有用。有任何想法吗?

My AlertDialog

我使用以下代码创建了 AlertDialog:

public class CustomInputDialog{

private OnDialogClickListener listener;
private Context context;
private String title;
private String message;
EditText input;
LinearLayout layout;

public interface OnDialogClickListener {
void onDialogOKClick(String value);
}

public CustomInputDialog(String title, String message, Context context, OnDialogClickListener listener) {

super();
this.title = title;
this.message = message;
this.context = context;
this.listener = listener;

layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 10, 20, 10);

input = new EditText(context);

InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(20);
input.setFilters(filters);

layout.addView(input, params);

}

private void dialog(){

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setView(layout);
builder.setTitle(title);
builder.setMessage(message);
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString();
listener.onDialogOKClick(value);
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}

}

最佳答案

您可以像这样以编程方式设置自定义 View ..

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

然后获取组件的引用

e.g. Button btn = (Button) dialoglayout.findViewById(R.id.button_id);

关于java - 如何更改 AlertDialog View 背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20742846/

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