第十一,十二周作业

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

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

   <EditText
       android:id="@+id/et_1"
       android:layout_width="300dp"
       android:layout_height="wrap_content"
       android:hint="请输入你想写的内容"
       android:textSize="80px"
       />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="写入"
        android:onClick="click1"
        android:textSize="80px"

        />


    <EditText
        android:id="@+id/et_2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="显示读取的内容"
        android:textSize="80px"
        />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="读取"
        android:onClick="click2"
        android:textSize="80px"

        />

</LinearLayout>
package com.example.a11121;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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 editText=findViewById(R.id.et_1);
        String fileName="data.txt";
        String content=editText.getText().toString();
        FileOutputStream fos=null;
        try {
            fos =openFileOutput(fileName,MODE_PRIVATE);
            fos.write(content.getBytes());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fos!=null){
                    fos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }


    public void click2(View view){
        EditText editText=findViewById(R.id.et_2);
        String content="";
        FileInputStream fis=null;
        try {
            fis=openFileInput("data.txt");
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            content=new String(buffer);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fis!=null){
                    fis.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        editText.setText(content);
        Toast.makeText(this, "读取成功", Toast.LENGTH_SHORT).show();

    }
}

 

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

 

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

    <TextView
        android:id="@+id/account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号:"
        android:textSize="30sp"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp"
        android:textColor="#000000"
        android:padding="10dp"
        />


    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/account"
        android:layout_marginTop="30dp"
        android:hint="请输入用户名"
        android:padding="10dp"
        android:textSize="30sp"
        />


    <TextView
        android:id="@+id/password"
        android:layout_below="@+id/account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:textSize="30sp"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp"
        android:textColor="#000000"
        android:padding="10dp"
        />


    <EditText
        android:id="@+id/et_2"
        android:layout_below="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/account"
        android:layout_marginTop="30dp"
        android:hint="请输入密码"
        android:padding="10dp"
        android:textSize="30sp"
        />

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:textSize="20sp"
        android:layout_below="@+id/et_2"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        />


    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:textSize="20sp"
        android:layout_below="@+id/et_2"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@+id/cb_1"
        />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="20sp"
        android:layout_below="@+id/et_2"
        android:layout_toRightOf="@+id/cb_2"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="20dp"
        />



</RelativeLayout>
package com.example.a11122;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements android.view.View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private EditText et_1;
    private EditText et_2;
    private Button btn_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Map<String,String> userInfo=SPSaveQQ.getUserInfo(this);
        if(userInfo!=null){
            et_1.setText(userInfo.get("account"));
            et_2.setText(userInfo.get("password"));
        }
    }

    private void initView() {
        et_1=findViewById(R.id.et_1);
        et_2=findViewById(R.id.et_2);
        btn_1=findViewById(R.id.btn_1);
        btn_1.setOnClickListener(this);
        CheckBox cb1=findViewById(R.id.cb_1);
        cb1.setOnCheckedChangeListener(this);
        CheckBox cb2=findViewById(R.id.cb_2);
        cb2.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_1:
                String account=et_1.getText().toString().trim();
                String password=et_2.getText().toString();
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()){
            case R.id.cb_1:
                String account=et_1.getText().toString().trim();
                String password=et_2.getText().toString();
                boolean isSaveSuccess=SPSaveQQ.saveUserInfo(this,account,password);
                if(isSaveSuccess&&b){
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                    et_1.setText(null);
                    et_2.setText(null);
                }
                break;
            case R.id.cb_2:
                account=et_1.getText().toString().trim();
                password=et_2.getText().toString();
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(this,"请输入账号",Toast.LENGTH_LONG).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_LONG).show();
                    return;
                }
                if(b)
                    Toast.makeText(this,"登录成功",Toast.LENGTH_LONG).show();
                else {
                    Toast.makeText(this,"请登录",Toast.LENGTH_LONG).show();
                }
        }
    }
}
package com.example.a11122;

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-10-30 22:49  请叫我妖玉大侠  阅读(27)  评论(0编辑  收藏  举报