• 记住账号密码(本地存储)


    1、activity_main.xml文件样式

    2、MainActivity.java主页内容

    3、创建一个保存文件的类

    1.activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    
        <ImageView
            android:id="@+id/iv"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:background="@drawable/dongman"/>
    
        <LinearLayout
            android:id="@+id/ll_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/iv"
            android:layout_centerVertical="true"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"
            android:background="#ffffff">
    
            <TextView
                android:id="@+id/tv_number"
                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_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@null"
                android:padding="10dp"/>
        </LinearLayout>
    
    
        <LinearLayout
            android:id="@+id/ll_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ll_number"
            android:layout_centerVertical="true"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"
            android:background="#ffffff">
    
            <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:padding="10dp"/>
        </LinearLayout>
        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ll_password"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="30dp"
            android:text="登录"
            android:background="#3C8DC4"
            android:textSize="20sp"/>
    
    
    </RelativeLayout>
    

    2.MainActivity.java主页内容

    package com.example.remembernp;
    
    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.util.Map;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        private EditText etNumber;
        private EditText etPassword;
        private Button btnLogin;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = (Button)findViewById(R.id.btn_login);
            Map<String,String> userInfo = SaveFile.getUserInfo(this);
            initView();
            if(userInfo != null){
                etNumber.setText(userInfo.get("number"));
                etPassword.setText(userInfo.get("password"));
            }
        }
    
        private void  initView(){
            etNumber = (EditText)findViewById(R.id.et_number);
            etPassword = (EditText)findViewById(R.id.et_password);
            btnLogin = (Button)findViewById(R.id.btn_login);
            btnLogin.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            //单击事件,获取账号密码
            String number = etNumber.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            //检查账号密码是否正确
            if(TextUtils.isEmpty(number)){
                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 = SaveFile.saveUserInfo(this,number,password);
            if(isSaveSuccess){
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
    

     3.创建一个保存文件的类

    package com.example.remembernp;
    
    import android.content.Context;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    public class SaveFile {
        //把账号密码保存在data.txt文件中
        public static boolean saveUserInfo(Context context, String number, String password){
            try{
                FileOutputStream fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
                fos.write((number + ":" + password).getBytes());
                fos.close();
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        //从data.txt中去获取刚刚保存的账号密码
        public static Map<String,String> getUserInfo(Context context) {
            String content = "";
            try {
                FileInputStream 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("number",infos[0]);
                userMap.put("password",infos[1]);
                fis.close();
                return userMap;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
    
        }
    }
    
  • 相关阅读:
    win2008R2、win7不停闪屏、程序失去响应的解决办法
    “我要上春晚”刷票器
    Windows Phone 7的About模板——Your Last About Dialog
    该伙伴事务管理器已经禁止了它对远程/网络事务的支持
    安装配置apache+php+mysql小结
    [摘]CSS的优先级探讨
    利用domdrag实现flash滚动条功能
    WIN7下如何做好IE6,7的兼容性测试
    同级情况下CSS的优先级探讨
    [NHibernate] NHibernate对象关系映射工具了解
  • 原文地址:https://www.cnblogs.com/Mr-Deng/p/11313106.html
Copyright © 2020-2023  润新知