【问题标题】:Disabling save button on AlertDialog unless input field is not blank除非输入字段不为空,否则禁用 AlertDialog 上的保存按钮
【发布时间】:2019-11-22 20:27:03
【问题描述】:

我们需要这个来禁止用户输入空值作为文件名。除非 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();
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    你需要在输入元素编辑文本中添加一个监听器

       AlertDialog.Builder builder = new AlertDialog.Builder(AudioRecorder.this);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle("Record Sound");
        builder.setMessage("Enter Username");
        builder.setPositiveButton("PositiveButton",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        // DO TASK
                    }
                });
        builder.setNegativeButton("NegativeButton",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        // DO TASK
                    }
                });
    
        // Set `EditText` to `dialog`. You can add `EditText` from `xml` too.
        final EditText userInput = new EditText(AudioRecorder.this);
    
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );
        input.setLayoutParams(lp);
    
    
        builder.setView(input);
    
        final AlertDialog dialog = builder.create();
        dialog.show();
    
        // Initially disable the button
        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    
        // OR you can use here setOnShowListener to disable button at first time.
    
        // Now set the textchange listener for edittext
        input.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
                // Check if edittext is empty
                if (TextUtils.isEmpty(s)) {
                    // Disable ok button
                    ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    
                } else {
                    // Something into edit text. Enable the button.
                    ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                }
    
            }
        });
    

    在文本更改方法上检查长度是否大于零并在那里分配按钮属性

    【讨论】:

      【解决方案2】:

      你可以这样做:

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

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

      【讨论】:

      • 如何禁用 .setPositiveButton("Save")?它不是一个普通的按钮。
      • 我会创建一个自定义对话框类来处理那里的所有内容,我将编辑我的答案
      • 我已经编辑了我的答案,请注意对于这个简单的任务,您可以像上面的答案一样使用TextChangedListener,但我想向您展示更复杂的任务的更简洁的解决方案。
      【解决方案3】:

      Add a TextChangedListener 到编辑文本。通过用户输入启用或禁用按钮。

      您可以使用dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);

      访问积极按钮

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 2016-10-26
        • 1970-01-01
        • 2011-07-12
        相关资源
        最近更新 更多