android登陆界面保存账号密码附带源码

今天学了登陆界面账号密码保存。小项目,麻烦老鸟给指点下!


1、画登陆界面UI

新建android Application project,在layout文件中添加UI界面代码,如下:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.day20170107.MainActivity" >

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/EditText1" />
    
    <EditText 
        android:id="@+id/et_password"
        android:layout_below="@id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="@string/EditText2"/>
    
    <CheckBox 
        android:id="@+id/cb_issave"
        android:layout_below="@id/et_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CheckBox"/>
    <Button 
        android:id="@+id/btn_login"
        android:layout_below="@id/et_password"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        />
        
     
	</RelativeLayout>

(不清楚请参考我之前的博客:点击打开链接)

2.添加按钮点击事件,代码如下:

public class MainActivity extends ActionBarActivity {

    private TextView username;
    private TextView password;
	private CheckBox checkBox;
	private Button btn_login;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化界面
        setContentView(R.layout.activity_main);
        username = (TextView) findViewById(R.id.et_username);
        password = (TextView) findViewById(R.id.et_password);
        checkBox = (CheckBox) findViewById(R.id.cb_issave);
        btn_login = (Button) findViewById(R.id.btn_login);
        //添加点击事件
        btn_login.setOnClickListener(new MyonclickListener());
        
            
        
    }
	private class MyonclickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			//获取EditText控件数据
			String user = username.getText().toString().trim();
			String pwd = password.getText().toString().trim();
			
			//判断输入是否为空
			if (TextUtils.isEmpty(user)||TextUtils.isEmpty(pwd)){
				
				Toast.makeText(MainActivity.this, "账号或密码输入为空", Toast.LENGTH_SHORT).show();
			}
			
		}
		
	}
}
(不清楚请参考:点击打开链接

3.保存账号密码信息,代码如下:

//这段代码在之前代码基础上添加 
private class MyonclickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			//获取EditText控件数据
			String user = username.getText().toString().trim();
			String pwd = password.getText().toString().trim();
			
			//判断输入是否为空
			if (TextUtils.isEmpty(user)||TextUtils.isEmpty(pwd)){
				
				Toast.makeText(MainActivity.this, "账号或密码输入为空", Toast.LENGTH_SHORT).show();
			
			}else {
				
				//获取CheckBox控件账号
				boolean checked = checkBox.isChecked();
				
				//判断是否需要保存信息
				if (checked) {
					
					//判断是否有成功保存信息
					//Util.SaveInfo(user,pwd)为自定义函数,使用方法见下文
					boolean saveinfo = Util.SaveInfo(user,pwd);
					if (saveinfo) {
						
						Toast.makeText(MainActivity.this, "用户信息保存成功", Toast.LENGTH_LONG).show();
					
					}else {
						
						Toast.makeText(MainActivity.this, "用户信息保存失败", Toast.LENGTH_LONG).show();
					}
					//打印
					Log.d("MainActivity", "保存用户名:"+user+"密码:"+pwd);	
					
				}
				//执行登陆的业务逻辑
				Log.d("MianActivity", "开始登陆。。。");
			
			}
			
		}
		
	}
 

Utils.SaveInfo(user,pwd)使用说明:因函数为自定义函数需自己添加类,方法如下:将鼠标移动到红线处,或选择Utils函数同时按“Ctrl+1”出现如图窗口,选择“Create class “Ultils””项回车,如图1

图1

按图2操作,创建类

图2

同理,为SaveInfo添加类方法。结果如下图

图3

Utils.SaveInfo类方法代码为:

package com.example.day20170107_1.Util;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;

import android.R.string;

public class Util {
/********************************************
 *功能:保存用户账号密码
 *********************************************/
	public static boolean SaveInfo(String user, String pwd) {
		
		// TODO Auto-generated method stub
		//保存方式
		String info = user + "##" +pwd;
		//保存信息位置并以txt文件方式
		File file = new File("data/data/com.example.day20170107_1/info.txt");
		try {
			
			//将用户信息写入info.txt文件
			FileOutputStream fis = new FileOutputStream(file);
			fis.write(info.getBytes());
			return true;		 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		
	}


}

调试运行可得,且在模拟器或手机的资源管理器data/data/com.example.day20170107_1/路径下可查看info.txt文件(需拷贝到其他地方查看):

图4

4、重开软件自动显示账号密码:

 在界面初始化出添加如下代码:

public class MainActivity extends ActionBarActivity {

    private TextView username;
    private TextView password;
	private CheckBox checkBox;
	private Button btn_login;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化界面
        setContentView(R.layout.activity_main);
        username = (TextView) findViewById(R.id.et_username);
        password = (TextView) findViewById(R.id.et_password);
        checkBox = (CheckBox) findViewById(R.id.cb_issave);
        btn_login = (Button) findViewById(R.id.btn_login);
        //添加点击事件
        btn_login.setOnClickListener(new MyonclickListener());
        
        //读取保存的信息
        //ReadInfo()类方法创建同saveInfo();
        String[] ReadInfo = Util.ReadInfo();
        //判断ReadInfo是否为空,如不为空,显示账号密码;
        if (ReadInfo != null) {
			
        	username.setText(ReadInfo[0]);
        	password.setText(ReadInfo[1]);
        	
		}
        
    }

 Util.ReadInfo();类方法代码:

/********************************************
	 *功能:读取账号密码
********************************************/
public static String[] ReadInfo() {
	// TODO Auto-generated method stub
	//获取保存文件
	File file = new File("data/data/com.example.day20170107_1/info.txt");
	//读取数据
	try {
		
		FileInputStream fis = new FileInputStream(file);
		 BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
		//切割函数
		String temp = reader.readLine();
		String[] result = temp.split("##");
		return result;
		
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		return null;
	}
	
}


图5

源码下载地址:点击打开链接




  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值