【问题标题】:how to check stored value using shared preferences in android?如何使用 android 中的共享首选项检查储值?
【发布时间】:2016-09-30 16:12:21
【问题描述】:

我在注册布局中有一个文本框,在登录布局中有一个文本框。现在如何使用 android 中的共享首选项验证文本框值?

【问题讨论】:

标签: java android android-layout local-storage sharedpreferences


【解决方案1】:

简单..

signupTextloginText 视为对象。

  1. 优先存储注册文本。 我已经使用了一个单独的类作为偏好。有关如何在单独的类中编写偏好并使用它的更多信息,请refer这里偏好..

优先存储 signupText。

mPreferenceData.storeInPreference("signup",     
signupText.getText().toString().trim()));

在登录页面中

String signUpText = mPreferenceData.getStoreInPreference("signup");
//now you will have data in signupText
button.setOnClickListener(new View.OnClickListener(){        
    if(loginText.getText().toString().trim().equalsIgnoreCase(signUpText)){
        // now loginText whatever data that matches with signUpText will make to login successful
    } else{
        //failure
    }

});

【讨论】:

  • 我没有得到。我有两个活动 MainActivity.java(SignUp) 和 LogIn.java 你能详细解释一下吗?
  • 在mainactivity中,输入文本存储在首选项中。然后在登录页面中,获取首选项并使用equalsIgnoreCase进行检查
【解决方案2】:

要比较不同活动中的值,首先需要将值存储在首选项中,如何在首选项中存储值检查Android User Session Management using Shared Preferences然后您可以比较值。

package com.androidhive.sessions;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

    // Sharedpref file name
    private static final String PREF_NAME = "AndroidHivePref";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";

    // User name (make variable public to access from outside)
    public static final String KEY_NAME = "name";

    // Email address (make variable public to access from outside)
    public static final String KEY_EMAIL = "email";

    // Constructor
    public SessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    /**
     * Create login session
     * */
    public void createLoginSession(String name, String email){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);

        // Storing name in pref
        editor.putString(KEY_NAME, name);

        // Storing email in pref
        editor.putString(KEY_EMAIL, email);

        // commit changes
        editor.commit();
    }   

    /**
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     * */
    public void checkLogin(){
        // Check login status
        if(!this.isLoggedIn()){
            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context, LoginActivity.class);
            // Closing all the Activities
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // Add new Flag to start new Activity
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // Staring Login Activity
            _context.startActivity(i);
        }

    }



    /**
     * Get stored session data
     * */
    public HashMap<String, String> getUserDetails(){
        HashMap<String, String> user = new HashMap<String, String>();
        // user name
        user.put(KEY_NAME, pref.getString(KEY_NAME, null));

        // user email id
        user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));

        // return user
        return user;
    }

    /**
     * Clear session details
     * */
    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();

        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, LoginActivity.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Staring Login Activity
        _context.startActivity(i);
    }

    /**
     * Quick check for login
     * **/
    // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    相关资源
    最近更新 更多