首页 > 解决方案 > 对话框按钮的可视化定制

问题描述

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

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

我们可以设置“保存”按钮的样式以显示红色文本吗?

标签: javaandroid

解决方案


你可以得到Button然后改变它的文本颜色。以下几行的东西应该可以工作,

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


推荐阅读