sharepreference儲存數據

1:

package com.itheima.sharepreference;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.itheima.sharepreference.service.LoginService;

public class SharepreferenceActivity extends Activity {
 private EditText et_username = null;
 private EditText et_password = null;
 private CheckBox cb_remeber_password = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  this.et_username = (EditText) this.findViewById(R.id.et_username);
  this.et_password = (EditText) this.findViewById(R.id.et_password);
  this.cb_remeber_password = (CheckBox) this
    .findViewById(R.id.cb_remember_psw);
  
  SharedPreferences sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
  this.et_username.setText(sp.getString("username", ""));
  this.et_password.setText(sp.getString("password", ""));
  
 }

 public void login(View view) {
  String username = this.et_username.getText().toString().trim();
  String password = this.et_password.getText().toString().trim();
  if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
   Toast.makeText(this, "用戶名或密碼不能爲空", 0).show();
  } else {
   if (this.cb_remeber_password.isChecked()) {
    LoginService.saveInfo(this, username, password);
     Toast.makeText(this, "保存密碼成功", 0).show();
    
   }
   if ("weijie".equals(username) && "123".equals(password)) {
    Toast.makeText(this, "登錄成功", 0).show();
   } else {
    Toast.makeText(this, "登錄失敗", 0).show();

   }
  }
 }
}

 

2:

package com.itheima.sharepreference.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class LoginService {

 public static void saveInfo(Context context, String username,
   String password) {
  SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
  Editor editor = sp.edit();
  editor.putString("username", username);
  editor.putString("password", password);
  editor.commit();
 }

}

 

3:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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" >

        <requestFocus />
    </EditText>

    <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="wrap_content"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/cb_remember_psw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="記住密碼" />

        <Button
            android:onClick="login"
            android:layout_alignParentRight="true"
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登錄" />
    </RelativeLayout>

</LinearLayout>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章