我有一个负责显示AlertDialog
的方法,它有一个包含EditText
的自定义布局,并单击OnPositive按钮。我试图获取编辑文本的值,但EditText.getText()
总是返回空。
码:
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));
}
}
});