我有一个带有编辑文本(自定义布局)的对话框片段。我想只在编辑文本不为空时才能点击“完成”按钮。打开对话框时,这不应该是可点击的。
这是我的DialogFragment类:
public class AddNumberDialog extends DialogFragment {
private Context mContext;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mContext = activity;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View mView = inflater.inflate(R.layout.dialog_add_number, null);
final EditText mEditText = (EditText)
mView.findViewById(R.id.number_edit_text);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setView(mView)
.setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String number = mEditText.getText().toString();
//Add number to list
}
})
.setNegativeButton(R.string.dialog_cancel, null);
return builder.create();
}
}
答案 0 :(得分:1)
您必须使用Dialog。因为警报对话框按钮在按下时关闭警报对话框。无需在OnClickListener中执行代码。 您可以阻止AlertDialog解除。请参阅this Answer if someone is interested。
覆盖onStart方法以设置Done Button Clickable False.
public class AddNumberDialog extends DialogFragment {
private Context mContext;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mContext = activity;
}
Button button; // Your done Button
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View mView = inflater.inflate(R.layout.dialog_add_number, null);
final EditText mEditText = (EditText)
mView.findViewById(R.id.number_edit_text);
mEditText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if(s.length()>0){
button.setEnabled(true);
}else{
button.setEnabled(false);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setView(mView)
.setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Do Nothing here... We Will be getting the Button Click Listener in Onstart
}
})
.setNegativeButton(R.string.dialog_cancel, null);
return builder.create();
}
@Override
public void onStart()
{
super.onStart(); //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point
AlertDialog d = (AlertDialog)getDialog();
if(d != null)
{
button= (Button) d.getButton(Dialog.BUTTON_NEGATIVE);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String number = mEditText.getText().toString();
//Add number to list
Boolean wantToCloseDialog = false;
//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
}
}
}