用SharedPreferences保存内容

本文介绍如何在Android应用中实现登录界面,并使用SharedPreferences来保存用户的登录信息,包括账号和密码,以便下次启动应用时自动填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同样先写好登录界面的布局:

<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/ed_zhanghao"
        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/ed_mima"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox 
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="记住密码"/>
        <Button 
            android:onClick="denglu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="登录"/>
    </RelativeLayout>

</LinearLayout>
界面如下图:

然后创建一个类用SharedPreferences方法保存账号密码:

package com.xh.tx.denglu.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

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

public class Servicein {
	public static void serviceinfo(Context context,String zhanghao,String mima){
		
		SharedPreferences sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
		//得到一个sp的编辑器
		Editor editor = sp.edit();
		editor.putString("zhanghao", zhanghao);
		editor.putString("mima", mima);
		//类似于数据库的事务,保证数据同时提交成功
		editor.commit();

	}
}
最后在主代码里写好,调出SharedPreferences保存的内容
package com.xh.tx.denglu;

import java.util.Map;

import com.xh.tx.SharePerference.R;
import com.xh.tx.denglu.service.Servicein;

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

public class MainActivity extends Activity {
	 private EditText ed_zhanghao;
	 private EditText ed_mima;
	 private CheckBox cb;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ed_zhanghao = (EditText) findViewById(R.id.ed_zhanghao);
		ed_mima = (EditText) findViewById(R.id.ed_mima);
		cb = (CheckBox) findViewById(R.id.cb);
		SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		String zhanghao=sp.getString("zhanghao", "");
		ed_zhanghao.setText(zhanghao);
		String mima=sp.getString("mima", "");
		ed_mima.setText(mima);
	}

	public void denglu(View v){
		String zhanghao = ed_zhanghao.getText().toString().trim();
		String mima = ed_mima.getText().toString().trim();
		if(TextUtils.isEmpty(zhanghao)||TextUtils.isEmpty(mima)){
			Toast.makeText(this, "账号或者密码不能为空", Toast.LENGTH_SHORT).show();
		}else{
			if(cb.isChecked()){
				Servicein.serviceinfo(this,zhanghao,mima);
				
				Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
				
			}
			if("zhangsan".equals(zhanghao)&&"123456".equals(mima)){
				Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
			}
		}

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值