android登录实现,存储数据到/data/data/包名/info.txt

摘要:
layout_ height=“wrap_content”&gt:layout_ alignParentRight=“true”/&gt:importandroid.app.Activity;privateEditTextet_name;privateEditTextet_ pwd;

1.一个简单登录界面布局代码如下:

@1采用线性布局加相对布局方式

@2线性布局采用垂直排列

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.market.login.MainActivity">

    <EditText
        android:id="@+id/et_name"
        android:layout_marginTop="150dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et_pwd"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_marginTop="20dp"
            android:layout_height="wrap_content">
            <CheckBox
                android:id="@+id/cb"
                android:layout_width="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_centerVertical="true"
                android:layout_height="wrap_content"
                android:text="是否记住密码"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="登录"
                android:layout_marginRight="50dp"
                android:layout_centerHorizontal="true"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
</LinearLayout>

界面效果如下:

android登录实现,存储数据到/data/data/包名/info.txt第1张

2.代码逻辑,分为MainActivity类和SaveUserInfo 工具类

分三步:初始化UI,初始化数据(加载),初始化控制器

@1主代码如下

package com.market.login;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

import static com.market.login.R.id.cb;

public class MainActivity extends Activity {

    private Button btn;
    private EditText et_name;
    private EditText et_pwd;
    private CheckBox cb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        loadData();


    }

    public void login(View v){
        String name = et_name.getText().toString().trim();
        String password = et_pwd.getText().toString().trim();
        if(TextUtils.isEmpty(name) || TextUtils.isEmpty(password)){
            Toast.makeText(this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
        }else{
            if(cb.isChecked()) {//如果保存密码和名
                //开始保存,保存到文件,下次进来先读取这个文件
                SaveUserInfo.saveInfo(name,password,true);
                //提交用户名和密码给服务器
                Log.v("Maintivity","提交用户名和密码给服务器");

            }else{
                SaveUserInfo.saveInfo("","",false);
                //直接提交用户名和密码给服务器
                Log.v("Maintivity","直接提交用户名和密码给服务器");
            }

        }

    }

    public void initView(){
        btn = (Button)findViewById(R.id.btn);
        et_name = (EditText)findViewById(R.id.et_name);
        et_pwd = (EditText)findViewById(R.id.et_pwd);
        cb = (CheckBox)findViewById(R.id.cb);

    }

    public void loadData(){
        Map<String, String> info = SaveUserInfo.readInfo();
        if(info != null){
            et_name.setText(info.get("name"));
            et_pwd.setText(info.get("password"));
            cb.setChecked(info.get("isChecked").equals("true"));//如果保存了信息,那他上次就是勾选的
        }

    }
}

@2保存数据到/data/data/包名/下文件的工具类

package com.market.login;

import android.util.Log;

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;

/**
 * 保存用户信息的类,实现保存用户信息到文件
 * Created by Administrator on 2017/6/14.
 */


public class SaveUserInfo {

    /*保存用户信息
    * name:用户名
    * password:密码
    * isChecked:是否勾选保存密码
    *
    * */
    public static boolean saveInfo(String name,String pwd,boolean isChecked){
        String info = name+"#"+pwd+"#"+isChecked;
        File file = new File("/data/data/com.market.login/info.txt");
        FileOutputStream fos = null;
        try {
             fos = new FileOutputStream(file);
            fos.write(info.getBytes());
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
    }

    /*读取用户信息*/
    public static Map<String,String> readInfo(){
        Map<String,String> map = null;
        File file = new File("/data/data/com.market.login/info.txt");
        if(!file.exists()){
            return map;
        }
        FileInputStream fis = null;
        BufferedReader br = null;
        try {
            fis = new FileInputStream(file);
             br = new BufferedReader(new InputStreamReader(fis));
            String info = br.readLine();
            Log.e("SaveUserInfo",info);
            String[] split = info.split("#");
            map = new HashMap<String,String>();
            map.put("name",split[0]);//保存读取的用户名和密码到map中
            map.put("password",split[1]);
            map.put("isChecked",split[2]);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return map;
        }
    }
}

3.运行效果,分三种情况

@1用户名和密码填写有空

android登录实现,存储数据到/data/data/包名/info.txt第2张

@2没有勾选checkbox效果和退出后重新登入效果

android登录实现,存储数据到/data/data/包名/info.txt第3张   android登录实现,存储数据到/data/data/包名/info.txt第4张

@3勾选后效果和退出重新登入效果

android登录实现,存储数据到/data/data/包名/info.txt第5张  android登录实现,存储数据到/data/data/包名/info.txt第6张

  

免责声明:文章转载自《android登录实现,存储数据到/data/data/包名/info.txt》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇android 原生应用、Web应用、混合应用优缺点分析App随机测试之Monkey和Maxim下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于adb安装指定版本

由于airtest测试群控安卓手机发现adb服务端和客户端版本不一致,运行经常报错,服务端是1.0.40 而客户端为1.0.41. (注意:服务端是指手机端,客户端为电脑端,安卓七和六的版本为40.而安卓八以上的为41,所以说做群控的时候最好安装同样传的手机系统) 此时我们需要更改客户端的adb版们,此处以mac为例 经多多方查找在这个人的博客上找到了 h...

Flutter的环境配置以及一些常见问题

flutter & AndroidStudio flutter的下载与配置 flutter是Google推出的基于Dart语言开发的跨平台开源UI框架,能够支持安卓与iOS。 flutter框架的下载地址为: Windows macOS Linux 若在上述网址中无法顺利下载,也可以去flutter的github下载,注意,github上flu...

查看Android应用包名、Activity的几个方法

转载自:http://blog.csdn.net/jlminghui/article/details/40622103 一、有源码情况 直接打开AndroidManifest.xml文件,找到包含android.intent.action.MAIN和android.intent.category.LAUNCHER对应的activity。 如下图中第三行pac...

launcher- 第三方应用图标替换

有时候我们感觉第三方应用的icon不美观,或者跟我们主题风格不一致,这时候我们希望换成我们想要的icon,那我们可以这么做(以更换QQ应用icon为例): 1.首先我们当然要根据自己的需要做一张替换icon了(图片我们不妨命名为qq) 2.接下来我们需要得到第三方应用的信息,可以通过GetDftlayoutXml.apk 工具获得 具体步骤如下 1)网上...

MongoDB副本集提高读写速率

一、提高副本集写的速率 1、通过设置Write Concern for Replica Sets¶ cfg = rs.conf()##cfg.settings.getLastErrorDefaults = { w: "majority", wtimeout: 5000 } cfg.settings.getLastErrorDefaults ={w:1}r...

Android控件系列之RadioButton&amp;amp;RadioGroup

学习目的: 1、掌握在Android中如何建立RadioGroup和RadioButton 2、掌握RadioGroup的常用属性 3、理解RadioButton和CheckBox的区别 4、掌握RadioGroup选中状态变换的事件(监听器) RadioButton和CheckBox的区别:1、单个RadioButton在选中后,通过点击无法变为未选中...