第五次Android作业

图片一 用内部存储实现文件写入和读取功能

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入你想写入的内容"/>
    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="导入"
        android:layout_below="@+id/et1"
        android:onClick="click1"/>
    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_1"
        android:hint="显示读取的内容"/>
    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取"
        android:layout_below="@+id/et2"
        android:onClick="click2"/>

</RelativeLayout>
package com.example.sq;

import androidx.appcompat.app.AppCompatActivity;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click1(View view){
        EditText editText1=(EditText)findViewById(R.id.et1);

        String filename="data.txt";
        String context=editText1.getText().toString();
        FileOutputStream fos=null;
        try {
            fos=openFileOutput(filename, MODE_PRIVATE);
            fos.write(context.getBytes());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }

    public void click2(View view){
        EditText editText2=(EditText)findViewById(R.id.et2);
        String context="";
        FileInputStream fis=null;
        try {
            fis=openFileInput("data.txt");
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            context=new String(buffer);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
        editText2.setText(context);
    }
}

 

图片二 使用sharedpreference实现记住密码功能

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号" />

    <EditText
        android:id="@+id/et_account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="密码" />

<EditText
    android:id="@+id/et_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="登录" />
</LinearLayout>
package com.example.sq;

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 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> userInfo = FileSaveQQ.getUserInfo(this);
        if (userInfo != null) {
            et_account.setText(userInfo.get("account"));
            et_password.setText(userInfo.get("password"));
        }
    }

    private void initView() {
        et_account = findViewById(R.id.et_account);
        et_password = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener((View.OnClickListener) this);
    }



    public void onClick(View view) {

        switch (view.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 = FileSaveQQ.saveUserInfo(this, account, password);
                if (isSaveSuccess) {
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }
}
package com.example.sq;
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 FileSaveQQ {
    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 com.example.sq;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;
public class SPSaveQQ {
    public static boolean saveUserInfo(Context context,String account,String password){
        SharedPreferences sp= context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor edit =sp.edit();
        edit.putString("Username",account);
        edit.putString("pwd",password);
        edit.commit();
        return true;
    }
    public static Map<String,String> getUserInfo(Context context){
        SharedPreferences sp =context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String account=sp.getString("username",null);
        String password=sp.getString("pwd",null);
        Map<String,String> userMap= new HashMap<String, String>();
        userMap.put("account",account);
        userMap.put("password",password);
        return userMap;
    }
}

 

posted @ 2021-11-01 19:08  星星邮寄者  阅读(26)  评论(0编辑  收藏  举报