gpt4 book ai didi

java - 禁用 AlertDialog 上的保存按钮,除非输入字段不为空

转载 作者:行者123 更新时间:2023-12-01 19:37:16 27 4
gpt4 key购买 nike

我们需要这个来禁止用户输入空值作为文件名。除非 userInput 不为空,否则应禁用保存按钮。

这是当前代码:

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());
Toast.makeText(AudioRecorder.this, "Next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
filePathMaking();
}
});
alertBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});

Dialog dialog = alertBuilder.create();
dialog.show();
}

最佳答案

你可以这样做:

if(input != null){
button.setEnabled(true); //you can click your button now
}else{
button.setEnabled(false); //you can not click your button
}

<小时/>

根据您的评论进行编辑:

以下是通用自定义对话框的示例:

这将是您的对话框类(或类似的东西,这只是一个示例):

public class FullSizeImageDialog extends Dialog {
private ImageView imageView;
private ProgressBar fullImageProgreesBar;
private Context dialogContext;

public FullSizeImageDialog(@NonNull Context context) {
super(context);
setContentView(R.layout.full_size_image_dialog);
dialogContext = context;
imageView = findViewById(R.id.full_size_image);
fullImageProgreesBar = findViewById(R.id.fullImageProgreesBar);
}
}

这是对话框的布局(在我的例子中是R.id.full_size_image):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66F9B639">


<!--Place your views here-->


</android.support.constraint.ConstraintLayout>

当您想显示对话框时,这非常简单:

FullSizeImageDialog dialog = new FullSizeImageDialog ();
dialog.show();

现在,您可以将逻辑放入自定义对话框类中。

关于java - 禁用 AlertDialog 上的保存按钮,除非输入字段不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57017650/

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