2
我有一個負責顯示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>
呃你是正確的 –
,但我無法設置EDITTEXT錯誤消息,當它進入else語句警告對話框沒有設置錯誤消息EDITTEXT –
@MehvishFaisal一旦你點擊按鈕,警報關閉了對話框會自動退出,並回調onClick監聽器 –