SharedPreferences的使用案例之QQ登录

来源:互联网 发布:qq显示ip软件 编辑:程序博客网 时间:2024/04/25 18:50

  在android手机中,可以通过一个“QQ登录”的案例来演示如何使用SharedPreferences存储数据。  

    1.创建一个名为“QQ登录”的应用程序,将包名修改为com.example.administrator.case_login,设计用户交互界面,布局文件如下:

      login_top.xml:

 

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@drawable/logintop_roundbg"    android:padding="@dimen/activity_horizontal_margin">    <EditText        android:id="@+id/etName"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:drawableLeft="@drawable/icon_user"        android:drawablePadding="10dp"        android:ems="10"        android:hint="@string/etName">        <requestFocus />    </EditText>    <EditText        android:id="@+id/etPassword"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/etName"        android:drawableLeft="@drawable/icon_pass"        android:drawablePadding="10dp"        android:ems="10"        android:hint="@string/etPass"        android:inputType="textPassword">        <requestFocus />    </EditText>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/etPassword">        <CheckBox            android:text="记住密码"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:id="@+id/cbIsRemeberPass"            android:textSize="20sp"            android:layout_weight="1" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@drawable/btn_select"            android:text="@string/btnLogin" />    </LinearLayout></RelativeLayout>
  activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:weightSum="1">    <TextView        android:text="Welcome you"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/textView"       android:layout_alignHorizontal="true"        android:textSize="40sp"/></LinearLayout>

布局文件中TextView用于提示用户名和密码的文字,EditText用于输入,最后在线性布局下方设置了一个按钮Button,用于单击保存用户名和密码。
  2.编写界面交互代码
  在loginActivity中,实现当用户输入完QQ号码和密码后,选择记住密码,单击“登录”按钮时调用Utils.saveInfo()方法保存QQ密码。
  
package com.example.administrator.case_login;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity{    private EditText etName;    private EditText etPassword;    private SharedPreferences sharePreferences;    private CheckBox cbIsRememberPass;    @Override    protected void onCreate(Bundle saveInstanceState){        super.onCreate(saveInstanceState);        setContentView(R.layout.activity_login);        initViews();        sharePreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);        boolean isRemember=sharePreferences.getBoolean("rememberpassword",false);        if(isRemember){            String name=sharePreferences.getString("name","");            String password=sharePreferences.getString("password","");            etName.setText(name);            etPassword.setText(password);            cbIsRememberPass.setChecked(true);        }    }public void initViews(){    etName=(EditText) findViewById(R.id.etName);    etPassword=(EditText) findViewById(R.id.etPassword);}    public void login(View view){    String name=etName.getText().toString();    String password=etPassword.getText().toString();    if ("admin".equals(name)&&"123456".equals(password)){        SharedPreferences.Editor editor=sharePreferences.edit();        if (cbIsRememberPass.isChecked()){         editor.putBoolean("rememberpassword",true);         editor.putString("name",name);         editor.putString("password",password);     }else{         editor.clear();     }        editor.commit();        Intent intent=new Intent(this,MainActivity.class);        startActivity(intent);        finish();     }else {        Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();    }    }}
 Mainactivity。java:
package com.example.administrator.case_login;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity{    @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

相关知识点:
使用SharedPreferences类存储数据时,首先需要通过conyext.getSharedPreferences(Stringname.int mode)获取SharedPreferences的实例对象。
SharedPreferences.Editor clear()--------------清空SharedPreferences中的所有数据
boolean.commit()---------------编辑结束,调用该方法提交
SharedPreferences.Editor.edit()---------------创建一个Editor对象
SharedPreferences.Editor remove(String key)-------------删除SharedPreferences指定key所对应的数据

阅读全文
0 0
原创粉丝点击