我有登录活动,我想在其余的活动中使用登录用户名和密码。我尝试过很多东西但没有运气
答案 0 :(得分:1)
一种方法是使用共享偏好:
save it in Preference in Activity C
SharedPreferences.Editor editor = getSharedPreferences("your_pref_name", MODE_PRIVATE).edit();
editor.putString("extra_name", extra_value);
editor.commit();
get it in Activity A
SharedPreferences prefs = getSharedPreferences("your_pref_name", MODE_PRIVATE);
String name = prefs.getString(""extra_name"", "");
答案 1 :(得分:0)
SharedPreferences settings = getSharedPreferences(ClassName.PREFS_NAME, 0); // 0 - for private mode
SharedPreferences.Editor editor = settings.edit();
//Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);
editor.putString("user_name",userid);
editor.putString("pass_word",pass);
// Commit the edits!
editor.commit();
使用相同的代码来获取数据...例如editor.getString(“user_name”,0);
希望它有效...对不起,如果我错了..我也是初学者......
答案 2 :(得分:0)
您可以使用SharedPreference保存和读取数据,如下所示:
var newFlightPath = [];
myLatLngs.forEach(function(item){
newFlightPath.push({
lat:item.latitude,
lng:item.longitude
})
})
答案 3 :(得分:0)
我认为用于存储用户数据的击球方式会创建单独的类和存储数据。下面我将为您提供如何使用单独的类的示例代码
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by Kintan Patel on 03-Aug-16.
*/
public class SessionHelper {
public static final String IS_LOGIN = "isLogin";
private static final String USER_NAME = "userName";
private static final String PASSWORD = "password";
private static final String SHARE_PREF = "spDemo";
private static SessionHelper mSessionHelper;
private Context context;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
public SessionHelper(Context context) {
this.context = context;
sharedPreferences = context.getSharedPreferences(SHARE_PREF, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
public void createLoginSession(String userName, String password) {
editor.putString(USER_NAME, userName);
editor.putString(PASSWORD, password);
editor.apply();
}
public String getUserName() {
return sharedPreferences.getString(USER_NAME, "");
}
}
使用SessionHelper类创建您想要使用的对象,如
SessionHelper helper = new SessionHelper(context);
helper.createLoginSession("userName","123");//add user data into shareprefrances
String userName = helper.getUserName(); //retrive userName from shareprefrances
您可以在任何活动中使用 getUserName()方法。