【问题标题】:check shared preference value检查共享偏好值
【发布时间】:2015-08-11 11:10:39
【问题描述】:

我想在每次启动应用程序时检查editext 中的值。我正在使用共享首选项来存储 edittext 值用户在第一次启动应用程序时输入的值。如果edittext 中的值与保存的共享首选项值相同,则下次启动应用程序时,应用程序不会显示该活动,而是跳过它并显示下一个活动。可能即使edittext 中的值正确,应用程序也不会跳过该活动。我认为比较值可能不正确。

MainActivity

public class LoginActivity extends Activity{

    EditText et;
    int keyDel;
    String tempString;
    char[] stringArray;

    SharedPreferences sharedpreferences;
    public static final String mypreference = "mypref";
    public static final String PIN = "passKey";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);

        et = (EditText)findViewById(R.id.editText1);

///////////////////////////////////// Shared Preference Code Starts /////////////////////////////////////////////////////////////////////////
        sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
        if (sharedpreferences.contains(PIN)) {
            et.setText(sharedpreferences.getString(PIN, ""));
        }

        String chk_et = et.getText().toString();
        if(chk_et == sharedpreferences.getString(PIN, "")){
            startActivity( new Intent(LoginActivity.this, Welcome.class));
            finish();

            //finish();
        }else{
            Toast.makeText(getApplicationContext(), "Wrong Details", Toast.LENGTH_SHORT).show();

        } 

///////////////////////////////////// Shared Preference Code Ends ///////////////////////////////////////////////////////////////////////////   

///////////////////////////////////// Text Watcher Code Starts //////////////////////////////////////////////////////////////////////////////
        et.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                 et.setOnKeyListener(new OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {

                              if(keyCode != KeyEvent.KEYCODE_DEL && et.getText().length() !=0)
                                {
                                    if(et.getText().length()==4 ||et.getText().length()==8)
                                    {                             
                                        tempString=et.getText().toString()+"-";
                                        char c=tempString.charAt(tempString.length()-2);

                                        if(c!='-')
                                        {
                                            stringArray = tempString.toCharArray();                    
                                            stringArray[tempString.length()-2]=stringArray[tempString.length()-1];
                                            stringArray[tempString.length()-1]=c;

                                            //code to convert charArray back to String..
                                            tempString=new String(stringArray);
                                            et.setText(tempString);             
                                            et.setSelection(tempString.length());
                                            tempString=null;
                                        }

                                    }
                                }

                                return false ;   
                        }
                    });


            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });

///////////////////////////////////// Text Watcher Code Ends //////////////////////////////////////////////////////////////////////////////     
    } // onCreate ends

    public void Save(View view) {
        String et_new = et.getText().toString();
        Editor editor = sharedpreferences.edit();
        editor.putString(PIN, et_new);
        editor.commit();
    }

} // Activity ends  

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    您不应将 String 对象与 == 运算符进行比较。

    当使用== 时,您实际上是在检查两个引用是否指向堆中的同一个字符串对象。

    你应该做的是将String与equals()方法进行比较,该方法比较String的内容。

    【讨论】:

      【解决方案2】:

      这个逻辑没有意义:

      if (sharedpreferences.contains(PIN)) {
                  et.setText(sharedpreferences.getString(PIN, ""));
              }
      
              String chk_et = et.getText().toString();
              if(chk_et == sharedpreferences.getString(PIN, "")){
                  startActivity( new Intent(LoginActivity.this, Welcome.class));
                  finish();
      
                  //finish();
              }else{
                  Toast.makeText(getApplicationContext(), "Wrong Details", Toast.LENGTH_SHORT).show();
      
              } 
      

      阅读文档!

      附:最好用equals()方法比较String值。

      【讨论】:

      • @Victor,我正在用这个逻辑检查 edittext 值是否与存储在共享首选项中的值相同。无论如何感谢您的建议。
      • @SomnathPal:是的,但您不应该使用== 来比较字符串。使用equals()方法比较一个字符串。
      • @Victor。知道了。使用 equals() 方法可以根据需要跳过布局。但问题是即使在我卸载该应用程序后,该应用程序也会跳过该活动。它不应跳过活动,因为在卸载应用程序后会清除 sharedpreference 值。
      • @Viktor。那么,代码正确吗?我只想将当前的 edittext 值与存储的 sharedpreference 值进行比较。
      • 要清除共享首选项,您需要清除应用数据,如果您卸载了您的应用,它不会被删除。转到设置->应用程序->选择您的应用程序->清除数据
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多