Android——Login用户登陆的设计实现

  我们生活中会遇到各种各样的登录界面,需要输入你的账号和密码,像登陆QQ呀,各种软件都需要这种设计,那我们今天来实现一下吧~~

首先,界面的设计如下:

  可以看出,我们需要两个可编辑文本框,用来输入用户名和密码,同时,我们要提示出要输入的类型,用hint来提示;我们需要一个checkbox来判断我们是否选择保存密码,还需要一个登陆button;由于整个线性布局是垂直的,因此两个可编辑文本框,一个checkbox,一个button在垂直方向上依次排列,这样不是很美观,因此我们需要再建立一个相对布局,使checkbox与button排列在一行。具体实现如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.administrator.login.login"
    tools:showIn="@layout/activity_login">

   <EditText
       android:id="@+id/mUserName"
       android:hint="用户名"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

    <EditText
        android:layout_below="@+id/mUserName"
        android:id="@+id/mPassWord"
        android:hint="密码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <CheckBox
            android:layout_centerVertical="true"
            android:id="@+id/mCheckBox"
            android:text="是否保存密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/mButton"
            android:text="登陆"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </RelativeLayout>

</LinearLayout>

设计完界面之后,我们来实现Activity:
实现用户登录界面需要几个步骤:

1、因为Activity一启动就会调用onCreate()函数,因此在onCreate()函数中获取界面中的各个控件,通过findViewById()函数。

2、为button设置一个监听器,当点下button并勾选checkbox时mCheckbox.isChecked(),获取用户名与密码,通过输出流输出到内部存储的info.txt文件中,为了方便读取,我们设置##来分割用户名与密码。

3、不管有无勾选checkbox,只有点击了button,我们就生成吐司提示界面,参数依次为:上下文环境(即当前Activity),要输出的字符串以及输出时间(3s或5s)

4、保存密码功能实现的是当用户退出程序,再一次进入时,已显示出用户名与密码,我们通过readfrominfo()方法进行实现。

5、readfrominfo()中,为了方便对数据的操作,我们首先要把文件字节流转化成输入流再转化为字符流,这样我们就可以调用字符流的readline()函数(即实现按行读取),若文件有多行,则设置循环while(下一行不为空){执行readline()},这样我们就把info.txt中的信息读到了字符串中了,我们把字符串中按##分割的各部分存入字符串数组中,则数组[0]为用户名,数组[1]为密码。

6、因为我们在一进入软件就会执行onCreate()函数,所以在这里面我们就要调用readfrominfo()函数,但是,如果用户是第一次登陆,并没有生成info.txt,直接调用readfrominfo()函数肯定会出错,因此在readfrominfo()函数中,我们要进行判断,判断info.txt文件是否存在file.exits()

代码如下:

package com.administrator.login;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class login extends AppCompatActivity {
    private EditText mUsername;
    private EditText mPassWord;
    String username=null;
    String password=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button mButton=(Button)findViewById(R.id.mButton);
        mUsername=(EditText)findViewById(R.id.mUserName);
        mPassWord=(EditText)findViewById(R.id.mPassWord);
        final CheckBox mCheckbox=(CheckBox)findViewById(R.id.mCheckBox);

        readfrominfo();     //调用读内部存储函数


        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCheckbox.isChecked()){
                    username=mUsername.getText().toString();
                    password=mPassWord.getText().toString();
                    File file = new File(getFilesDir(),"info.txt");     //写内部存储到info.txt文件
                    FileOutputStream fos= null;     //创建输出流
                    try {
                        fos = new FileOutputStream(file);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    try {
                        fos.write((username+"##"+password).getBytes());     //将username##password按位写入文件中
                        fos.close();                                           //关闭流
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                Toast.makeText(login.this,"登陆成功",Toast.LENGTH_SHORT).show();        //吐司界面,参数依次为提示发出Activity,提示内容,提示时长
            }
        });

    }

    //读内部存储函数
    public void readfrominfo(){
        File file = new File(getFilesDir(),"info.txt");
        if(file.exists()){
            BufferedReader br= null;
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));      //将字节流转化为输入流然后转化为字符流
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String str = null;
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String [] up= str.split("##");      //将字符串按##分割的部分依次存入字符串型数组up中

            mUsername.setText(up[0]);       //填写username与password
            mPassWord.setText(up[1]);
        }
    }
}

输出结果:

 

 

  

转载于:https://www.cnblogs.com/soada/p/5693693.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值