java - 对话框按钮的视觉定制

标签 java android

我在我的应用程序中使用了一个对话框,该对话框会弹出并与用户交互。我以前没有使用过对话框,所以我对它们的样式几乎一无所知。这是代码:

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"));
}

关于java - 对话框按钮的视觉定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57136139/

相关文章:

java - 使用 RedisTemplate zSet 使用通配符获取所有值并从 Redis 中删除所有值

Java 小程序菜单

java - recyclerview.onBindviewholder 始终处于位置 0

android - 如何测试 Kotlin Coroutine actors

android - getDrawingCache 未更新

java - 该实例的 Akka Actor 路径

java - 从 REST 资源中的数据库结果集返回 XML

java - 如何在一段时间间隔后生成新的随机整数?

android - Android Activity 类中的面向对象范围和继承

java - 将 Java Soap 客户端连接到启用 SSL 的连接