对话框中 Spinner 和 EditText 不为空时启用按钮
Posted
技术标签:
【中文标题】对话框中 Spinner 和 EditText 不为空时启用按钮【英文标题】:Enable Button when Spinner and EditText are not empty in Dialog 【发布时间】:2019-08-04 22:07:38 【问题描述】:在我的 AlertDialog 中,我有一个 Spinner 和一个 EditText。如果
,我正在尝试禁用对话框的肯定按钮-
在 Spinner 中选择的项目是“选择货币”(“选择货币”是我的数组中的第一个项目,并且 Spinner 设置为将字符串货币名称返回为“”)或
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)
);
【问题讨论】:
【参考方案1】:请尝试以下代码,只进行一项更改,将下面的行放在 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)
);
【讨论】:
不错的一个。坚持下去 alertDialog 在 onItemSelected 中不可用,如下所述! 请全局显示警报对话框。我已经编辑了我的答案。 感谢您的快速回复。我刚刚尝试了答案并遇到以下错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.lang.String.isEmpty()'错误在“alertDialog.getButton(AlertDialog .BUTTON_POSITIVE).setEnabled(!exchangeRateInput.isEmpty() && !currencyName.isEmpty());"在“currencyName = parent.getItemAtPosition(position).toString();”之后 已解决。我在一开始就声明了 exchangeRateInput = ""。感谢您的帮助!【参考方案2】:您应该调用微调器的按钮启用/禁用逻辑Spinner.onItemSelected
。到目前为止,您只检查 EditText.onTextChanged
中的逻辑。
-
在
Spinner
和EditText
上设置侦听器之前声明您的AlertDialog
。
在Spinner.onItemSelected
中添加您的逻辑以启用或禁用按钮。
在EditText.onTextChanged
中添加您的逻辑以启用或禁用按钮。
【讨论】:
【参考方案3】:如果你改变你的代码,你可以让你的代码工作,
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)
);
->这将完成你的工作。
【讨论】:
以上是关于对话框中 Spinner 和 EditText 不为空时启用按钮的主要内容,如果未能解决你的问题,请参考以下文章