星空网 > 软件开发 > 操作系统

SharedPreferences实现记住密码功能

SharedPerferences 简单介绍

  • 用于保存简单的键值对数据;
  • 它将数据放在 /data/data/<package name>/shared_prefs目录下,用MAP键值对;

SharedPerferences 使用步骤

将数据存储到SharedPerferences中:

  1.先要得到SharedPerference对象:(三种方法)

      1).使用Context类中的 getSharedPreferences() 方法,它接收两个参数,第一个参数为文件名称,第二个参数为操作模式。

       操作模式MODE_PRAVITE :只有当前程序才能对这个文件进行读写。MODE_MULTI_PROCESS :多个进程中对同一个文件进行读写。

       如:

SharedPreferences spf = getSharedPreferences("data",Context.MODE_PRIVATE);

 

      2).使用Activity类中的 getPreferences() 方法,它只接收一个参数--操作模式,并且会将当前活动的类名作为文件名。

       如:

SharedPreferences spf = getPreferences(MODE_PRIVATE);

 

      3).使用PreferenceManager类中的 getDefaultSharedPreferences() 方法,它接收一个Context参数,并且用包名作为前缀来命名文件。

       如:

SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);

 

  2.再得到SharedPreferences.Editor对象:

      使用SharedPreferences对象的 edit() 方法。

SharedPreference.Editor editor = spf.edit();

 

  3.开始添加数据:

      以键值对的方式写入数据。

editor.putInt("age",22);editor.putString("name","Visen");editor.putBoolean("singleDog",false)

 

  4.提交操作:

editor.commit();

 

从SharedPerferences中读取数据:

  提供了一系列的get方法进行数据的读取。如:

String name = editor.getString("name"," ");

 

  如果键所对应的值不存在,则填入设定的默认值。

 

 

简单的保存密码功能

login.

SharedPreferences实现记住密码功能images/loading.gif' data-original="http://images2015.cnblogs.com/blog/935742/201605/935742-20160516170118341-1464522505.png" width="193" height="343" />

<?  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@color/black"  android:stretchColumns="1">  <LinearLayout    android:layout_height="wrap_content"    android:background="@color/black"    android:orientation="vertical">    <ImageView      android:layout_width="match_parent"      android:layout_height="240dp"      android:src='/images/loading.gif' data-original="@drawable/image1"/>    <TextView      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:text="@string/title"      android:textSize="40sp"      android:textColor="@color/red"      android:gravity="center"      android:background="@color/cyan"/>  </LinearLayout>  <TableRow    android:layout_marginTop="30dp">    <TextView      android:layout_height="wrap_content"      android:text="@string/account"      android:textSize="30sp"      android:textColor="@color/white"/>    <EditText      android:id="@+id/account"      android:layout_height="wrap_content"      android:inputType="text"      android:textSize="20sp"      android:textColor="@color/red"      android:gravity="center"      android:singleLine="true"/>  </TableRow>  <TableRow>    <TextView      android:layout_height="wrap_content"      android:text="@string/password"      android:textSize="30sp"      android:textColor="@color/white"/>    <EditText      android:id="@+id/passWord"      android:layout_height="wrap_content"      android:inputType="textPassword"      android:textSize="20sp"      android:textColor="@color/red"      android:gravity="center" />  </TableRow>  <TableLayout    android:layout_height="wrap_content"    android:stretchColumns="0">    <TableRow>      <CheckBox        android:id="@+id/saveSelect"        android:background="@color/red"        android:layout_gravity="end"/>      <TextView        android:layout_height="wrap_content"        android:text="@string/saveSelect"        android:textSize="20sp"        android:textColor="@color/white"        android:gravity="center"        android:layout_gravity="bottom"/>    </TableRow>    <TableRow>      <Button        android:layout_height="wrap_content"        android:id="@+id/login"        android:gravity="center"        android:layout_span="2"        android:text="@string/login"        android:textSize="25sp"        android:textColor="@color/red"        android:background="@drawable/black_bt"/>    </TableRow>  </TableLayout></TableLayout>

 

Login.java

public class Login extends AppCompatActivity {  private SharedPreferences spf;  private SharedPreferences.Editor spfe;  private int num = 0;  private EditText account = null;  private EditText passWord = null;  private CheckBox saveSelect = null;  private Button login = null ;  @Override  protected void onCreate(Bundle saveInstanceState){    //加载布局    super.onCreate(saveInstanceState);    setContentView(R.layout.login);    //初始化控件    account = (EditText)findViewById(R.id.account);    passWord = (EditText)findViewById(R.id.passWord);    saveSelect = (CheckBox)findViewById(R.id.saveSelect);    login = (Button)findViewById(R.id.login);    //使用Context的getSharedPreferences(String name,int mode)方法得到SharedPreferences对象;    spf = getSharedPreferences("data", Context.MODE_PRIVATE);    //使用SharedPreferences对象的edit()方法得到 SharedPreferences.Editor 的对象;    spfe = spf.edit();    //复选框是否被选中,若为选中状态,则保存过账户,要恢复数据    if(spf.getBoolean("isSelect",false)){//选中标志,默认值为false      String acc = spf.getString("account","");      String pas = spf.getString("passWord","");      account.setText(acc);      passWord.setText(pas);      saveSelect.setChecked(true);    }    //设置登录按钮监听事件    login.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        //确认帐号密码        if(account.getText().toString().equals("visen") && passWord.getText().toString().equals("dsy402645063!")){          //复选框是否被勾选,若被勾选,则需要保存账户后登录;否则直接登录且不保存账户          if(saveSelect.isChecked()){             saveDate();          }else {            spfe.clear();            spfe.commit();          }                    //页面跳转          Intent intent = new Intent(Login.this,MainActivity.class);          startActivity(intent);          finish();        }else {//账户或密码错误          Toast.makeText(Login.this, "account or password is invalid", Toast.LENGTH_SHORT).show();        }      }    });  }  public void saveDate(){    //读取EditText中的内容    String acc = account.getText().toString();    String pas = passWord.getText().toString();    //保存数据    spfe.putString("account",acc);    spfe.putString("passWord",pas);    spfe.putBoolean("isSelect",true);    //提交    spfe.commit();  }    @Override  public void onBackPressed(){    num++;    if(num == 2){      super.onBackPressed();    }else{      Toast.makeText(Login.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();    }  }}

SharedPreferences实现记住密码功能SharedPreferences实现记住密码功能

 




原标题:SharedPreferences实现记住密码功能

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

聊聊电商视觉:https://www.goluckyvip.com/tag/876.html
重磅优惠:https://www.goluckyvip.com/tag/8760.html
GMV增长5%:https://www.goluckyvip.com/tag/8761.html
影响曝光率:https://www.goluckyvip.com/tag/8765.html
零售巨头倒下:https://www.goluckyvip.com/tag/8766.html
产品价格设计:https://www.goluckyvip.com/tag/8767.html
川藏线自驾游要怎么走才比较划算呢?:https://www.vstour.cn/a/411240.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流