课堂随笔_ide

 XML

<?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="com.example.hp.MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="输入你想写入的内容"/>

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

    <EditText
        android:id="@+id/et2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="显示读取的内容" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取"/>
</LinearLayout>

逻辑代码

package com.example.hp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.util.logging.Handler;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt1).setOnClickListener(this);
        findViewById(R.id.bt2).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt1:
                String fileName="data.txt";
                String content=((EditText)findViewById(R.id.et1)).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();
                    }
                }
                break;
            case R.id.bt2:
                content = "";
                FileInputStream fis=null;
                try{
                    fis=openFileInput("data.txt");
                    byte[] buffer=new byte[fis.available()];
                    fis.read(buffer);
                    content=new String(buffer);
                    ((EditText)findViewById(R.id.et2)).setText(content);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try{
                        if (fis != null) {
                            fis.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}

 

 

课堂随笔_java_02  课堂随笔_java_03  课堂随笔_android_04

 XML

 

<?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="com.example.hp.saveqq.MainActivity"
    android:orientation="vertical">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:src="@drawable/head"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:padding="10dp"/>
        <EditText
            android:id="@+id/et1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:hint="请输入用户名"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:padding="10dp"/>
        <EditText
            android:id="@+id/et2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:inputType="textPassword"
            android:hint="请输入密码"/>
    </LinearLayout>

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#3c8dc4"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:text="登录"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"/>

</LinearLayout>

逻辑代码

  工具类:

package com.example.hp.saveqq;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by HP on 2021/10/26.
 */

public class FileSaveQQ {
    public static boolean saveUserInfo(Context context,String name,String password){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("name",name);
        editor.putString("password",password);
        editor.commit();
        return true;
    }
    public static Map<String,String> getUserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String name=sp.getString("name",null);
        String password=sp.getString("password",null);
        Map<String,String>userMap=new HashMap<String, String>();
        userMap.put("name",name);
        userMap.put("password",password);
        return userMap;
    }
}

  界面交互

package com.example.hp.saveqq;

import android.content.SharedPreferences;
import android.support.v7.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 et1;
    private EditText et2;
    private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Map<String,String>userInfo=FileSaveQQ.getUserInfo(this);
        et1.setText(userInfo.get("name"));
        et2.setText(userInfo.get("password"));
    }

    private void initView() {
         et1=(EditText)findViewById(R.id.et1);
         et2=(EditText)findViewById(R.id.et2);
         bt=(Button)findViewById(R.id.bt);
         bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt:
                String name=et1.getText().toString();
                String password=et2.getText().toString();
                if(TextUtils.isEmpty(name)){
                    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,name,password);
                if (isSaveSuccess){
                    Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "保存失败!", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}