Java 对话框按钮的可视化自定义
我在应用程序中使用一个弹出的对话框,并与用户进行交互。我以前没有使用过对话框,所以我对它们的样式几乎一无所知。代码如下:Java 对话框按钮的可视化自定义,java,android,Java,Android,我在应用程序中使用一个弹出的对话框,并与用户进行交互。我以前没有使用过对话框,所以我对它们的样式几乎一无所知。代码如下: public void openDialog() { @SuppressLint("InflateParams") View view = (LayoutInflater.from(AudioRecorder.this)).inflate(R.layout.audio_name_input, null); AlertDialog.Builder alertB
public void openDialog() {
@SuppressLint("InflateParams") View view = (LayoutInflater.from(AudioRecorder.this)).inflate(R.layout.audio_name_input, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(AudioRecorder.this);
alertBuilder.setView(view);
final EditText userInput = view.findViewById(R.id.userInput);
alertBuilder.setCancelable(true);
alertBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
inputName = String.valueOf(userInput.getText());
if (!inputName.isEmpty()) {
Toast.makeText(AudioRecorder.this, "Next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
filePathMaking();
} else {
inputName = "recorded_audio";
Toast.makeText(AudioRecorder.this, "Input field empty, next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
}
}
});
alertBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
Dialog dialog = alertBuilder.create();
dialog.show();
}
我们可以设置保存按钮的样式以显示红色文本吗?您可以获取该按钮,然后更改其文本颜色。以下几点应该行得通
public void openDialog() {
@SuppressLint("InflateParams") View view = (LayoutInflater.from(AudioRecorder.this)).inflate(R.layout.audio_name_input, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(AudioRecorder.this);
alertBuilder.setView(view);
final EditText userInput = view.findViewById(R.id.userInput);
alertBuilder.setCancelable(true);
alertBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
inputName = String.valueOf(userInput.getText());
if (!inputName.isEmpty()) {
Toast.makeText(AudioRecorder.this, "Next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
filePathMaking();
} else {
inputName = "recorded_audio";
Toast.makeText(AudioRecorder.this, "Input field empty, next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
}
}
});
alertBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
Dialog dialog = alertBuilder.create();
dialog.show();
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextColor(Color.parseColor("#FF0B8B42"));
}
您可以获取按钮,然后更改其文本颜色。以下几点应该行得通
public void openDialog() {
@SuppressLint("InflateParams") View view = (LayoutInflater.from(AudioRecorder.this)).inflate(R.layout.audio_name_input, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(AudioRecorder.this);
alertBuilder.setView(view);
final EditText userInput = view.findViewById(R.id.userInput);
alertBuilder.setCancelable(true);
alertBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
inputName = String.valueOf(userInput.getText());
if (!inputName.isEmpty()) {
Toast.makeText(AudioRecorder.this, "Next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
filePathMaking();
} else {
inputName = "recorded_audio";
Toast.makeText(AudioRecorder.this, "Input field empty, next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
}
}
});
alertBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
Dialog dialog = alertBuilder.create();
dialog.show();
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextColor(Color.parseColor("#FF0B8B42"));
}
您可以按照Chrisvin Jem在回答中的建议使用AlertDialog,但我想提供另一种解决方案:
您可以创建一个自定义对话框类,以便为您的对话框提供自定义布局,在单独的类中控制所有内容-我发现它更干净、更有组织
例如,创建dialogClass:
您只需创建dialog instant并按如下方式调用它:
为什么我建议使用此对话框而不是AlertDialog.Builder:
您可以使用自定义对话框以更快的方式构建布局
当您可以自定义布局时,不需要编写大量代码来添加视图
我相信你更容易看到MyColdialog.show;而不是在一个方法中使用50行或更多的代码
是否需要更改有关对话框外观和代码的任何内容?很好,转到单独的类并对其进行更改,而不是在活动中再添加20行代码
您可以按照Chrisvin Jem在回答中的建议使用AlertDialog,但我想提供另一种解决方案:
您可以创建一个自定义对话框类,以便为您的对话框提供自定义布局,在单独的类中控制所有内容-我发现它更干净、更有组织
例如,创建dialogClass:
您只需创建dialog instant并按如下方式调用它:
为什么我建议使用此对话框而不是AlertDialog.Builder:
您可以使用自定义对话框以更快的方式构建布局
当您可以自定义布局时,不需要编写大量代码来添加视图
我相信你更容易看到MyColdialog.show;而不是在一个方法中使用50行或更多的代码
是否需要更改有关对话框外观和代码的任何内容?很好,转到单独的类并对其进行更改,而不是在活动中再添加20行代码
Chrisvin Jem对您的问题给出了extact答案,但是如果您想要对设计进行更多控制,您可以使用此代码
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.yourview);
RelativeLayout submit = dialog.findViewById(R.id.submit);
final EditText edittext = dialog.findViewById(R.id.edittext);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(context, getString(R.string.thanks), Toast.LENGTH_SHORT).show();
}
});
dialog.show();
Chrisvin Jem对您的问题给出了extact答案,但是如果您想要对设计进行更多控制,您可以使用此代码
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.yourview);
RelativeLayout submit = dialog.findViewById(R.id.submit);
final EditText edittext = dialog.findViewById(R.id.edittext);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(context, getString(R.string.thanks), Toast.LENGTH_SHORT).show();
}
});
dialog.show();