温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Android 中使用SharedPreferences实现保存登录数据功能

发布时间:2021-05-31 17:21:11 来源:亿速云 阅读:119 作者:Leah 栏目:移动开发

这篇文章给大家介绍怎么在Android 中使用SharedPreferences实现保存登录数据功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1.activity_main.xml页面存放所有的控件,我在每一行都使用了线性布局。

activity_main.xml页面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginLeft="20dp"
 android:layout_marginRight="20dp"
 tools:context=".SecondActivity" >
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:orientation="horizontal" >
 
  <TextView
   android:id="@+id/tvName"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="用户名:" />
 
  <EditText
   android:id="@+id/etInputName"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="2" />
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="60dp"
  android:orientation="horizontal" >
 
  <TextView
   android:id="@+id/tvPass"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="密 码:" />
 
  <EditText
   android:id="@+id/etInputPass"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="2" />
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="100dp"
  android:orientation="horizontal" >
 
  <CheckBox
   android:id="@+id/cbSave"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:checked="false"
   android:text="保存用户名" />
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="130dp"
  android:orientation="horizontal" >
 
  <Button
   android:id="@+id/btLogin"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="登录" />
 
  <Button
   android:id="@+id/btCancel"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="取消" />
 </LinearLayout>
 
</RelativeLayout>

2.MainActivity.java页面处理登录和保存数据。

MainActivity.java页面:

package com.example.sharedpreferences;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener{
 
 SharedPreferences pref;
 Editor editor;
 private EditText etInputName,etInputPass;
 private CheckBox ckSave;
 private Button btLogin,btCancel;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 /**
 * 获取控件id
 */
 getId();
 /**
 * 保存数据
 */
 saveData();
 /**
 * 绑定点击事件
 */
 bindClick();
 }
 
 
 
 /**
 * 获取控件id
 */
 private void getId() {
 etInputName=(EditText) findViewById(R.id.etInputName);
 etInputPass=(EditText) findViewById(R.id.etInputPass);
 ckSave=(CheckBox) findViewById(R.id.cbSave);
 btLogin=(Button) findViewById(R.id.btLogin);
 btCancel=(Button) findViewById(R.id.btCancel);
 }
 /**
 * 保存数据
 */
 private void saveData() {
 
 //获得SharedPreferences对象
 pref=getSharedPreferences("userInfo",MODE_PRIVATE);//将内容存放到名为userInfo的文档内
 
 //获得SharedPreferences.Editor对象
 editor=pref.edit();
 
 String name=pref.getString("userName","");//获取用户名
 if(name.equals("")){//如果name为空,代表未选择保存用户名
 ckSave.setChecked(false);//不勾选
 }else{
 ckSave.setChecked(true);
 etInputName.setText(name);//将读取到的name值赋值到EditText中
 }
 }
 
 /**
 * 绑定点击事件
 */
 private void bindClick() {
 btLogin.setOnClickListener(this);
 btCancel.setOnClickListener(this);
 }
 
 /**
 * 按钮点击事件
 */
 @Override
 public void onClick(View view) {
 switch (view.getId()) {
 case R.id.btLogin:
 String name=etInputName.getText().toString().trim();//获取输入的名字并去掉空格
 String pass=etInputPass.getText().toString().trim();//获取输入的密码并去掉空格
 if("admin".equals(name)&&"123456".equals(pass)){
 if(ckSave.isChecked()){//如果选择保存用户名
  editor.putString("userName",name);
  editor.commit();//提交数据
 }else{//如果未选择保存用户名
  editor.remove("userName");//删除用户名
  editor.commit();//提交数据(每次更改都需要提交)
 }
 Toast.makeText(SecondActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
 }else{
 Toast.makeText(SecondActivity.this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
 }
 break;
 case R.id.btCancel:
 break;
 }
 }
}

3.保存的文件目录可以查看的到,点击右上角进入,找到data->data->当前目录的包名->shared-prefs->新建的文件名

怎么在Android 中使用SharedPreferences实现保存登录数据功能

4.另外,点击右上角导出可以暂时保存到桌面,然后选择打开方式可以查看里边信息。

5.还有一点是,当程序在真机上运行时,file explore打不开data文件夹,根据网上的经验,真机先root,然后在手机上装上R.E 管理器(或类似软件),将/data/data的权限修改为可读可写可执行,然后,就可以在eclipse中展开了。

怎么在Android 中使用SharedPreferences实现保存登录数据功能

6.SharedPreferences多用于配置信息或者内容较少的数据的保存,当数据量复杂或者较大,还是需要使用数据库。

关于怎么在Android 中使用SharedPreferences实现保存登录数据功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI