无法从EditText中检索字符串

时间:2014-07-23 19:19:30

标签: android string android-xml

我试图找出为什么我无法从两个edittexts获取有效字符串

以下几行是我所说的

String newPin = etNewPassword.getText().toString();
String conPin = etConfirmPassword.getText().toString();

以下是整个文件

public class PasswordDialogPreference extends DialogPreference {

    TextView tvOldPassword;
    TextView tvNewPassword;
    TextView tvConfirmPassword;

    EditText etOldPassword;
    EditText etNewPassword;
    EditText etConfirmPassword;

    SharedPreferences sharedPrefs;
    SharedPreferences.Editor editor;

    public PasswordDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.password_dialog_preference);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
        setDialogIcon(null);
        setPersistent(false);
    }

    @Override
    public void onBindDialogView(View view) {
        tvOldPassword = (TextView) view.findViewById(R.id.tvOldPassword);
        tvNewPassword = (TextView) view.findViewById(R.id.tvNewPassword);
        tvConfirmPassword = (TextView) view.findViewById(R.id.tvConfirmPassword);

        etOldPassword = (EditText) view.findViewById(R.id.etOldPassword);
        etNewPassword = (EditText) view.findViewById(R.id.etOldPassword);
        etConfirmPassword = (EditText) view.findViewById(R.id.etOldPassword);

        sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(getContext());


        if (sharedPrefs.getString("prefPasscode", "").length() < 4) {
            tvOldPassword.setVisibility(View.GONE);
            etOldPassword.setVisibility(View.GONE);
        }
        super.onBindDialogView(view);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);
        //builder.setNegativeButton(null, null);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sharedPrefs = PreferenceManager
                        .getDefaultSharedPreferences(getContext());
                String oldPin;
                String newPin = etNewPassword.getText().toString();
                String conPin = etConfirmPassword.getText().toString();

                Log.d("pins", etNewPassword.getText().toString() +  " " + etConfirmPassword.getText().toString());

                if (newPin.equals(conPin)) {
                    //editor.putString("prefPasscode", etConfirmPassword.getText().toString());
                    //editor.commit();
                    Toast.makeText(getContext(), "Pin saved." + conPin,
                            Toast.LENGTH_SHORT
                    ).show();
                } else if (etNewPassword.getText().toString().length() !=  4) {
                    Toast.makeText(getContext(), "Pin must be 4 digits.",
                            Toast.LENGTH_SHORT
                    ).show();
                } else if (!etNewPassword.getText().toString().equals(etConfirmPassword.getText().toString())) {
                    Toast.makeText(getContext(), "Pin does not match.",
                            Toast.LENGTH_SHORT
                    ).show();
                }
            }
        });
    }

}

以下是edittext布局,我不确定inputtype:numberpassword是否导致任何问题

<EditText
                android:id="@+id/etNewPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:maxLength="4"
                android:inputType="numberPassword"
                android:width="100dp"
                android:key="keyNewPasscode"/>

<EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/etConfirmPassword"
                android:maxLength="4"
                android:textSize="15dp"
                android:inputType="numberPassword"
                android:width="100dp"
                android:key="keyConfirmPasscode"/>

1 个答案:

答案 0 :(得分:1)

您有类似的观点ID:

etOldPassword = (EditText) view.findViewById(R.id.etOldPassword);
etNewPassword = (EditText) view.findViewById(R.id.etOldPassword);
etConfirmPassword = (EditText) view.findViewById(R.id.etOldPassword);