在Android中隐藏DialogFragment中的按钮
我已经实现了这样的对话框片段在Android中隐藏DialogFragment中的按钮,android,button,visibility,android-dialogfragment,Android,Button,Visibility,Android Dialogfragment,我已经实现了这样的对话框片段 public class SessionExpiredFragment extends DialogFragment { public interface SessionExpiredFragmentListener { public void onCancelLoginProcessPressed(DialogFragment dialog); // validValues = true if fields are no
public class SessionExpiredFragment extends DialogFragment {
public interface SessionExpiredFragmentListener {
public void onCancelLoginProcessPressed(DialogFragment dialog);
// validValues = true if fields are not empty and email is a valid
// email,
// else validValues = false;
public void onOKLoginProcessPressed(DialogFragment dialog,
boolean validValues);
}
SessionExpiredFragmentListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (SessionExpiredFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement SessionExpiredFragmentListener");
}
}
// UI references
private EditText mEmailView;
private EditText mPasswordView;
private View mLoginFormView;
private View mLoginStatusView;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.login_fragment_layout, null);
/*
* Get edit texts references
*/
mEmailView = (EditText) view.findViewById(R.id.email);
mPasswordView = (EditText) view.findViewById(R.id.password);
mLoginFormView = view.findViewById(R.id.login_form);
mLoginStatusView = view.findViewById(R.id.login_status);
/*
* Set builder values
*/
builder.setMessage(R.string.session_expired_title)
.setView(view)
.setPositiveButton(R.string.action_ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
boolean validValues = true;
String email = mEmailView.getText().toString();
String password = mPasswordView.getText()
.toString();
if (TextUtils.isEmpty(email)
|| TextUtils.isEmpty(password))
validValues = false;
if (!isValidEmail(email))
validValues = false;
mListener.onOKLoginProcessPressed(
SessionExpiredFragment.this,
validValues);
}
})
.setNegativeButton(R.string.action_cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
mListener
.onCancelLoginProcessPressed(SessionExpiredFragment.this);
}
});
return builder.create();
}
它有正负两个按钮。我需要做的是在按下正极按钮时隐藏它们。我使用这个监听器,所以我可以在我的活动中听,但这对我也没有帮助。如何(以及在何处添加代码)隐藏按钮?谢谢你的帮助。 然后你应该考虑创建自定义视图并通过代码管理按钮的可见性,只需重写DealScript中的Stand(),并保持对该按钮的引用。您可以稍后使用引用重新启用按钮:
Button negativeButton = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
buttonNo.setEnabled(false);
Button positiveButton;
@Override
public void onStart() {
super.onStart();
AlertDialog d = (AlertDialog) getDialog();
if (d != null) {
positiveButton = (Button)d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);
}
}
请参见此处:这如何帮助我处理不属于布局的按钮-按钮是使用.setPositiveButton和.setNegativeButton添加的。如果我要使用findById查找他们,他们的id是什么?我应该在哪个视图上调用findById?我会将此标记为答案,但这需要在onStart()方法中完成,否则按钮将为空。我们如何从
SessionExpiredFragment
的实例中获取alert
?