• 登陆界面


    acivity界面代码

    <?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> 

    MainActivity界面:

    package com.example.dll;
     
    import android.app.Activity;
    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 Activity 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.action_settings);
            et_password=(EditText) findViewById(R.id.action_settings);
            btn_login=(Button) findViewById(R.id.action_settings);
            btn_login.setOnClickListener(this);
        }
     
        public void onClick(View v){
            switch (v.getId()){
                case R.id.action_settings:
                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;
            }
        }
    }

    file界面:

    package com.example.dll;
    
    import android.content.Context;
    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();
                }
            }
        }
    }

    截屏:

  • 相关阅读:
    LAMP----linux+apache+mysql+php详细安装步骤之一APACHE篇(openldap等)
    Apache2 httpd.conf 配置详解
    Apache+php+mysql的安装与配置
    linux PHP 安装及 GD库安装
    Linux下安装PHP的GD支持库
    linux下tar.gz、tar、bz2、zip等解压缩、压缩命令
    Android中Context详解 ---- 你所不知道的Context(转)
    Android简单文件浏览器源代码 (转)
    Android入门之文件系统操作(一)简单的文件浏览器 (转)
    Android入门之文件系统操作
  • 原文地址:https://www.cnblogs.com/zhangjiatong/p/14037979.html
Copyright © 2020-2023  润新知