本文介绍了无法从检索的EditText字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去弄清楚为什么我无法从两个edittexts得到一个有效的字符串

以下行是什么,我说什么

 字符串newPin = etNewPassword.getText()的toString()。
字符串conPin = etConfirmPassword.getText()的toString()。

下面是整个文件

 公共类PasswordDialog preference扩展对话框$ P $ {pference    TextView的tvOldPassword;
    TextView的tvNewPassword;
    TextView的tvConfirmPassword;    的EditText etOldPassword;
    的EditText etNewPassword;
    的EditText etConfirmPassword;    共享preferences共享preFS;
    共享preferences.Editor编辑;    公共PasswordDialog preference(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);        setDialogLayoutResource(R.layout.password_dialog_ preference);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
        setDialogIcon(NULL);
        setPersistent(假);
    }    @覆盖
    公共无效onBindDialogView(查看视图){
        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);        共享preFS = preferenceManager
                .getDefaultShared preferences(的getContext());
        如果(共享prefs.getString(prefPass code,)。长度()4;){
            tvOldPassword.setVisibility(View.GONE);
            etOldPassword.setVisibility(View.GONE);
        }
        super.onBindDialogView(视图);
    }    @覆盖
    保护无效onDialogClosed(布尔positiveResult){
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }    @覆盖
    在prepareDialogBu​​ilder(AlertDialog.Builder生成器)保护无效{
        super.on prepareDialogBu​​ilder(制造商);
        //builder.setNegativeButton(null,NULL);
        builder.setPositiveButton(android.R.string.ok,新DialogInterface.OnClickListener(){
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                共享preFS = preferenceManager
                        .getDefaultShared preferences(的getContext());
                串oldPin;
                。字符串newPin = etNewPassword.getText()的toString();
                字符串conPin = etConfirmPassword.getText()的toString()。                Log.d(销,etNewPassword.getText()的toString()++ etConfirmPassword.getText()的toString());                如果(newPin.equals(conPin)){
                    。//editor.putString(\"$p$pfPass$c$c,etConfirmPassword.getText()的toString());
                    //editor.commit();
                    Toast.makeText(的getContext(),针得救了。+ conPin,
                            Toast.LENGTH_SHORT
                    )。显示();
                }否则如果(etNewPassword.getText()。的toString()。长度()!= 4){
                    Toast.makeText(的getContext(),PIN必须是4位。
                            Toast.LENGTH_SHORT
                    )。显示();
                }否则如果(!etNewPassword.getText()。的toString()。等于(etConfirmPassword.getText()。的toString())){
                    Toast.makeText(的getContext(),针不匹配。
                            Toast.LENGTH_SHORT
                    )。显示();
                }
            }
        });
    }}

下面是EditText上的布局,即时通讯不知道的inputType:numberpassword引起的任何问题。

 <的EditText
                机器人:ID =@ + ID / etNewPassword
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =15dp
                机器人:最大长度=4
                安卓的inputType =numberPassword
                机器人:宽=100dp
                机器人:键=keyNewPass code/><的EditText
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:ID =@ + ID / etConfirmPassword
                机器人:最大长度=4
                机器人:TEXTSIZE =15dp
                安卓的inputType =numberPassword
                机器人:宽=100dp
                机器人:键=keyConfirmPass code/>


解决方案

您有类似的看法IDS:

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

Im trying to figure out why I am unable to get a valid string from two edittexts

The following lines are what I am talking about

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

Below is the entire file

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();
                }
            }
        });
    }

}

Below are the edittext layouts, im not sure if inputtype: numberpassword is causing any issues

<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"/>
解决方案

You have similar ids of views:

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

这篇关于无法从检索的EditText字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:35