android基础(八):SharedPreferences原理

android基础(8):SharedPreferences原理

一:界面设计

<LinearLayoutxmlns: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:orientation="vertical"

    tools:context=".MainActivity">

    <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

        android:text="请输入用户名" />

       <EditText

          android:id="@+id/et_username"

          android:layout_width="match_parent"

       android:layout_height="wrap_content"

          />

       <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

        android:text="请输入密码" />

       <EditText

          android:id="@+id/et_password"

          android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:inputType="textPassword"

          />

       <RelativeLayout

          android:layout_width="fill_parent"

       android:layout_height="wrap_content"

          >

          <CheckBox

              android:id="@+id/cb_rember_pwd"

              android:checked="true"

              android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text="记住密码"

              />

          <Button

              android:layout_alignParentRight="true"

              android:onClick="login"

              android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text="登陆"

              ></Button>

       </RelativeLayout>

</LinearLayout>

 

二:SharedPreferences存入数据

 

import android.content.Context;

importandroid.content.SharedPreferences;

importandroid.content.SharedPreferences.Editor;

public classLoginService {

       /**

        * 保存用户名密码的业务方法

        *@param context

        *@param username

        *@param password

        */

       public static void saveUserInfo(Contextcontext, String username,String password){

              SharedPreferencessp=context.getSharedPreferences("config",Context.MODE_PRIVATE);

              //得到sp的编辑器

              Editor edit=sp.edit();

              edit.putString("username",username);

              edit.putString("password",password);

              //类似于数据库的事物,保证同时提交

              edit.commit();

       }

}

 

该方法生成config.xml文件

<?xmlversion='1.0' encoding='utf-8' standalone='yes' ?>

<map> 

<stringname="password">123</string>

<stringname="username">spring</string>

</map>

 

三:获取保存的数据

public classMainActivity extends Activity {

       private static final String tag ="MainActivity";

       private EditText et_username;

       private EditText et_password;

       private CheckBox cb;

       @Override

       protected void onCreate(BundlesavedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

              //必须在布局文件下面初始化控件

              et_username=(EditText)findViewById(R.id.et_username);

              et_password=(EditText)findViewById(R.id.et_password);

              cb=(CheckBox) findViewById(R.id.cb_rember_pwd);

             

              //检查是否有保存用户名和密码,如果有这回显

              SharedPreferencessp=getSharedPreferences("config", MODE_PRIVATE);

              Stringusername=sp.getString("username", "");

              et_username.setText(username);

              Stringpassword=sp.getString("password", "");

              et_password.setText(password);

       }

       //登陆按钮的点击事件

       public void login(View view){

              Stringusername=et_username.getText().toString().trim();

              Stringpassword=et_password.getText().toString().trim();

              if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){

                     Toast.makeText(this, "用户名或者密码为空!", Toast.LENGTH_SHORT).show();

              }

              else{

                     //判断是否保存密码

                     if(cb.isChecked()){

                            //保存用户名和密码

                            Log.i(tag, "需要保存用户名和密码!");

                            LoginService.saveUserInfo(this,username,password);

                            Toast.makeText(this,"信息保存成功!", Toast.LENGTH_SHORT).show();

                     }

                     //验证用户名和密码是否正确

                     if("spring".equals(username)&&"123".equals(password)){

                            Toast.makeText(this,"登陆成功!", Toast.LENGTH_SHORT).show();

                     }else{

                            Toast.makeText(this,"登陆失败!", Toast.LENGTH_SHORT).show();

                     }

              }

       }

}

 

 

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21