Android笔记 SharedPreferences demo

来源:互联网 发布:单片机433接收程序 编辑:程序博客网 时间:2024/06/05 08:23

1布局

<LinearLayout 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:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入用户名" />    <EditText        android:id="@+id/etusername"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入用密码" />    <EditText        android:id="@+id/etpass"        android:layout_width="fill_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_rem_pass"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="记住密码" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:onClick="login"            android:text="登陆" />    </RelativeLayout></LinearLayout>

2代码

MainActivity.java

package com.example.a24_logform;import com.example.a24_logform.lognservice.LoginService;import com.example.a28_sharedpreference.R;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private static final String TAG="MainActivity";EditText editname =null;EditText editpass=null;CheckBox checkBox=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editname=(EditText) this.findViewById(R.id.etusername);editpass=(EditText) this.findViewById(R.id.etpass);checkBox=(CheckBox) this.findViewById(R.id.cb_rem_pass);SharedPreferences sp= getSharedPreferences("config", MODE_PRIVATE);String name=sp.getString("name", "");editname.setText(name);String pass=sp.getString("pass", "");editpass.setText(pass);}public void login(View view){String nameString=editname.getText().toString().trim();String passString=editpass.getText().toString().trim();if(TextUtils.isEmpty(nameString)||TextUtils.isEmpty(passString)){Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();}else{if(checkBox.isChecked()){Log.i(TAG, "要保存密码");Toast.makeText(this, "记住密码", Toast.LENGTH_LONG).show();LoginService.saveUserInfo(this,nameString, passString);Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();}if("zhangsan".equals(nameString)&&"123".equals(passString)){Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();}else{Toast.makeText(this, "登陆失败,用户名或密码错误", Toast.LENGTH_LONG).show();}}}}


LoginService.java

package com.example.a24_logform.lognservice;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class LoginService {public static void saveUserInfo(Context context, String name,String pass){SharedPreferences sp=context.getSharedPreferences("config", Context.MODE_PRIVATE);Editor editor= sp.edit();editor.putString("name", name);editor.putString("pass", pass);//采用类似事物的回滚 保证数据同时提交editor.putInt("count", 23);editor.putFloat("PI", 3.1415f);editor.commit();}}


0 0
原创粉丝点击