gpt4 book ai didi

android - 如何禁用/启用对话框负正按钮?

转载 作者:IT老高 更新时间:2023-10-28 13:03:14 24 4
gpt4 key购买 nike

请查看下面的自定义对话框。我在对话框中有一个 edittext 字段,如果文本字段为空,我想禁用 positiveButton。我可以获得文本字段的 charListener,但我不确定如何设置 positivebutton 以禁用或启用该监听器?正面和负面按钮的引用是什么?

 case DIALOG_TEXT_ENTRY:
// This example shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
}

最佳答案

编辑完整的解决方案...

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.");
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 input = new EditText(MainActivity.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);
}

}
});

Below are edited history, which can be refer as some more details

这是一个示例代码,试试这个

AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//DO TASK
}
});
builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//DO TASK
}
});

AlertDialog dialog = builder.create();
dialog.show();

// After calling show method, you need to check your condition and enable/disable the dialog buttons
if (your_condition_true) {
// BUTTON1 is the positive button
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);
}

对于否定按钮

dialog.getButton(AlertDialog.BUTTON2).setEnabled(false); //BUTTON2 is negative button

按钮 id:引用 alert_dialog.xml

已编辑:

还有 setOnShowListener从第 8 级 API (FroYo) 开始,也是如此,

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, null);

AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {

@Override
public void onShow(DialogInterface dialog) {
if (condition) {
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
});

dialog.show();

已编辑

new AlertDialog.Builder(this)
.setMessage("This may take a while")
.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
// the rest of your stuff
}

}).show();

关于android - 如何禁用/启用对话框负正按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238952/

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