登录界面

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e6e6e6"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:src="@drawable/ic_launcher_background"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="账号:"
            android:textColor="#000"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码:"
            android:textColor="#000"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:inputType="textPassword"
            android:padding="10dp"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3c8dc4"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>

</androidx.appcompat.widget.LinearLayoutCompat> 
package cn.itcast.dl;

import android.content.Context;
import androidx.annotation.Nullable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class file {
    public static boolean saveUserInfo(Context context,String account,String password){
        FileOutputStream fos=null;
        try{
            fos=context.openFileOutput("data.txt",Context.MODE_PRIVATE);
            fos.write((account + ":"+password).getBytes());
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }finally {
            try {
                if (fos!=null){
                    fos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    public static Map<String,String> getUserInfo(Context context){
        String content="";
        FileInputStream fis=null;
        try{
            fis=context.openFileInput("data.txt");
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            content=new String(buffer);
            Map<String,String> usermap=new HashMap<String, String>();
            String[] infos=content.split(":");
            usermap.put("account",infos[0]);
            usermap.put("password",infos[1]);
            return usermap;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try{
                if (fis!=null){
                    fis.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
package cn.itcast.dl;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_account;
    private EditText et_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Map<String,String> userInfon= file.getUserInfo(this);
        if(userInfon!=null){
            et_account.setText(userInfon.get("account"));
            et_password.setText(userInfon.get("password"));
        }
    }
    private void initView(){
        et_account=(EditText) findViewById(R.id.et_account);
        et_password=(EditText) findViewById(R.id.et_password);
        btn_login=(Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_login:
            String account=et_account.getText().toString().trim();
            String password=et_password.getText().toString();
            if(TextUtils.isEmpty(account)){
                Toast.makeText(this,"请输入账号:",Toast.LENGTH_SHORT).show();
                return;
            }
            if(TextUtils.isEmpty(password)){
                Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
            boolean isSaveSuccess=file.saveUserInfo(this,account,password);
            if(isSaveSuccess){
                Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }
}

  

  

posted @ 2020-11-19 20:18  无法显示  阅读(112)  评论(0编辑  收藏  举报