第十一十二周作业

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

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;

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

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){
        String filename = "data.txt";
        EditText edt = findViewById(R.id.edt_1);
        String content = edt.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,content,1).show();
    }

    public void click2(View view){
        TextView tv = findViewById(R.id.tv_1);
        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();
            }
        }
        tv.setText(content);
    }
}
<?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">

    <EditText
        android:id="@+id/edt_1"
        android:layout_width="500px"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click1"
        android:text="写入" />

    <EditText
        android:id="@+id/tv_1"
        android:layout_width="500px"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click2"
        android:text="读取" />

</LinearLayout>

 

 2.图片二 使用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/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:text="账号" />

    <EditText
        android:id="@+id/et1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@id/tv1"
        android:hint="请输入用户名" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et1"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="13dp"
        android:text="密码" />

    <EditText
        android:id="@+id/et2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/et1"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="-2dp"
        android:layout_toRightOf="@id/tv2"
        android:hint="请输入密码" />

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textColor="#E4061B"
        android:background="#501683"
        android:layout_below="@id/cb1"
        android:layout_marginLeft="150dp"/>
    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:layout_below="@id/et2"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="50dp"/>

    <CheckBox
        android:id="@+id/cb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et2"
        android:layout_marginLeft="58dp"
        android:layout_marginTop="6dp"
        android:layout_toRightOf="@id/cb1"
        android:text="自动登录" />

</LinearLayout>

 

posted @ 2021-11-04 22:00  Vending  阅读(19)  评论(0编辑  收藏  举报