当 Spinner 和 EditText 在 Dialog 中不为空时启用 Button

Enable Button when Spinner and EditText are not empty in Dialog

在我的 AlertDialog 中,我有一个 Spinner 和一个 EditText。如果

,我正在尝试禁用对话框的肯定按钮
  1. 在 Spinner 中选择的项目是 "Select currency"("Select currency" 是我的数组中的第一个项目,Spinner 设置为 return String currencyName as "")或
  2. EditText 为空。

在下面的代码中,如果我在 Spinner 中选择项目之前在 EditText 中输入,则肯定按钮保持禁用状态。我不知道如何让这两个一起工作。

任何帮助将不胜感激。谢谢

private void openDialog(){
    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    View subView = inflater.inflate(R.layout.dialog_layout, null);
    final EditText subEditTextExchangeRate = subView.findViewById(R.id.textInputExchangeRate);
    final Spinner subSpinner = subView.findViewById(R.id.spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    subSpinner.setAdapter(adapter);
    subSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if(parent.getItemAtPosition(position).equals("Select currency")){
                currencyName = "";

            }else {
                currencyName = parent.getItemAtPosition(position).toString();
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(subView);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            exchangeRate = subEditTextExchangeRate.getText().toString();
            st2 = exchangeRate;
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });


    final AlertDialog alertDialog = builder.create();
    alertDialog.show();

    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    subEditTextExchangeRate.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String exchangeRateInput = subEditTextExchangeRate.getText().toString();
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty()); 
        }//This is the logic that I am looking for but the method is watching only EditText

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

您应该调用微调器的按钮 enable/disable 逻辑 Spinner.onItemSelected。截至目前,您仅检查 EditText.onTextChanged.

中的逻辑
  1. SpinnerEditText 上设置侦听器之前声明您的 AlertDialog
  2. Spinner.onItemSelected 中添加您的逻辑以启用或禁用按钮。
  3. EditText.onTextChanged 中添加您的逻辑以启用或禁用按钮。

请尝试下面的代码,只做一处更改,将下面的行放在 subSpinner.setOnItemSelectedListener 中,同时请全局声明 alertDialog。

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());

这是您的代码。

private void openDialog(){
    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    View subView = inflater.inflate(R.layout.dialog_layout, null);
    final EditText subEditTextExchangeRate = subView.findViewById(R.id.textInputExchangeRate);
    final Spinner subSpinner = subView.findViewById(R.id.spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    subSpinner.setAdapter(adapter);
    subSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if(parent.getItemAtPosition(position).equals("Select currency")){
                currencyName = "";

            }else {
                currencyName = parent.getItemAtPosition(position).toString();
            }
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty()); 
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(subView);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            exchangeRate = subEditTextExchangeRate.getText().toString();
            st2 = exchangeRate;
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });


    alertDialog = builder.create();
    alertDialog.show();

    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    subEditTextExchangeRate.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String exchangeRateInput = subEditTextExchangeRate.getText().toString();
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty()); 
        }//This is the logic that I am looking for but the method is watching only EditText

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

如果您像这样更改代码,就可以让您的代码工作,

    Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setEnabled(false);
        subEditTextExchangeRate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.length>0 && !TextUtils.isEmpty(currencyName )){
            button.setEnabled(true);
            // your logic
           }else{
            button.setEnabled(false);
            // your logic
            }


            @Override
            public void afterTextChanged(Editable s) {

            }
        });

->这会完成你的工作。