lovejobs

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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>

界面效果如下:

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用户名和密码填写有空

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

   

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

  

 

  

 

posted on 2017-06-14 10:41  lovejobs  阅读(1958)  评论(0编辑  收藏  举报