AlertDialog自定义布局视图不可访问android
问题描述:
我有一个负责显示AlertDialog
的方法,它有一个自定义布局,其中包括一个EditText
和OnPositive按钮单击我试图获取编辑文本的值,但EditText.getText()
总是返回空。AlertDialog自定义布局视图不可访问android
代码:
private void showForgetPassDialog() {
final View view = getLayoutInflater().inflate(R.layout.alert_dialog_forgot_pass, null);
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setCancelable(false);
builder.setTitle(getString(R.string.forgot_password));
builder.setMessage(getString(R.string.reset_pass));
final EditText emailTxt = (EditText) view.findViewById(R.id.email_field);
emailReset = emailTxt.getText().toString();
String positiveText = getString(R.string.submit);
builder.setPositiveButton(positiveText,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(LoginActivity.this, "test :"+emailReset, Toast.LENGTH_SHORT).show();
if(!emailReset.equals("") && isValidEmail(emailReset)){
resetPassword(emailReset);
}else{
emailTxt.setError(getString(R.string.error_email));
}
}
});
String negativeText = getString(R.string.cancel);
builder.setNegativeButton(negativeText,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(LoginActivity.this,R.color.colorAccentDark));
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(LoginActivity.this,R.color.PrimaryBlue));
}
});
dialog.setView(view);
// display dialog
dialog.show();
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
>
<EditText
android:textCursorDrawable="@null"
android:id="@+id/email_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="@string/email_label"
android:inputType="textEmailAddress"
android:textColor="#484848" />
<View
android:id="@+id/login_bar"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/email_field"
android:background="#e1e1e1" />
</RelativeLayout>
答
你需要移动
emailReset = emailTxt.getText().toString();
到的onClick监听器内。
builder.setPositiveButton(positiveText,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Here!!!!!!!!!
emailReset = emailTxt.getText().toString();
Toast.makeText(LoginActivity.this, "test :"+emailReset, Toast.LENGTH_SHORT).show();
if(!emailReset.equals("") && isValidEmail(emailReset)){
resetPassword(emailReset);
}else{
emailTxt.setError(getString(R.string.error_email));
}
}
});
呃你是正确的 –
,但我无法设置EDITTEXT错误消息,当它进入else语句警告对话框没有设置错误消息EDITTEXT –
@MehvishFaisal一旦你点击按钮,警报关闭了对话框会自动退出,并回调onClick监听器 –