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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textSize="20sp" /> <EditText android:id="@+id/user" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:textSize="20sp" /> <EditText android:id="@+id/pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> </LinearLayout> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="保存密码" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:onClick="Click" android:textSize="20sp"/> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:onClick="Click" android:textSize="20sp"/> </LinearLayout> </LinearLayout>
源代码:
package com.example.sharepreferencesdemo; import java.util.prefs.Preferences; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; /** * 利用preferences保存登录的用户名 * @author Administrator * */ public class sharePreferences_example extends Activity{ private EditText user; private EditText pass; private Button login; private Button cancel; private CheckBox check; private Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.pref_examle); user = (EditText) findViewById(R.id.user); pass = (EditText) findViewById(R.id.pass); login = (Button) findViewById(R.id.login); cancel = (Button) findViewById(R.id.cancel); check = (CheckBox) findViewById(R.id.check); SharedPreferences pref = getSharedPreferences("userInfo",MODE_PRIVATE); editor = pref.edit(); String name = pref.getString("userName", ""); if(name.equals("")){ check.setChecked(false); }else{ check.setChecked(true); user.setText(name); } } public void Click(View view){ String userName = user.getText().toString().trim(); String passWord = pass.getText().toString(); switch(view.getId()){ case R.id.login: if("zhangsan".equals(userName)&&"123456".equals(passWord)){ Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); if(check.isChecked()){ editor.putString("userName", userName); editor.commit(); }else{ editor.remove("userName"); editor.commit(); } }else{ Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show(); } break; case R.id.cancel: break; } } }
时间: 2024-11-28 18:08:51