以XML的方式保存密码

来源:互联网 发布:上海市行知实验中学 编辑:程序博客网 时间:2024/06/08 13:35
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/et_usename"        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:inputType="textPassword"        android:id="@+id/password"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <CheckBox            android:id="@+id/cb_remenber_passward"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="记住密码" />        <Button            android:layout_alignParentRight="true"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="login"            android:text="登陆" >        </Button>    </RelativeLayout></LinearLayout>

2:主类的代码:

package com.example.shenchao2;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import com.example.shenchao2.service.LoginService;public class MainActivity extends Activity {private static final String TAG = "MainActivity";private EditText ed_usename;private EditText ed_password;private CheckBox cb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ed_usename = (EditText) this.findViewById(R.id.et_usename);ed_password = (EditText) this.findViewById(R.id.password);cb = (CheckBox) this.findViewById(R.id.cb_remenber_passward);SharedPreferences sp = getSharedPreferences("config1", MODE_PRIVATE);    String usename = sp.getString("usename", "");    ed_usename.setText("usename");    String password = sp.getString("password", "");    ed_password.setText("password");}/** * 登陆成功 *  * @param vew */public void login(View vew) {String usename = ed_usename.getText().toString().trim();String password = ed_password.getText().toString().trim(); // 将其内容拿出来// 判断是否为空if (TextUtils.isEmpty(usename) || TextUtils.isEmpty(password)) {Toast.makeText(this, "不能为空", Toast.LENGTH_LONG).show();} else {// 登陆,是否保存密码if (cb.isChecked()) {// 保存用户密码Log.i(TAG, "需要保存用户密码!");// 日志文件信息LoginService.saveUseInfo(this, usename, password);Toast.makeText(this, "保存用户信息成功!", Toast.LENGTH_LONG).show();}// 登陆发送消到服务器,服务器验证是否正确if ("zhangsan".equals(usename) && "123".equals(password)) {Toast.makeText(this, "登陆成功!", Toast.LENGTH_LONG).show();} else {Toast.makeText(this, "登陆失败!", Toast.LENGTH_LONG).show();}}}}

3:LoginService(保存方法的代码):

package com.example.shenchao2.service;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class LoginService {public static void saveUseInfo(Context context, String usename,String password) {// 文件的名字、文件访问的模式SharedPreferences sp = context.getSharedPreferences("config1",Context.MODE_PRIVATE);// 得到一个sp的编辑器Editor editor = sp.edit();editor.putString("usename", usename);editor.putString("password", password);editor.putBoolean("shenchao", true);editor.putFloat("heshiyu",123456f);editor.commit();//提交数据}}


0 0