第十一周 十二周作业

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edt_1"
        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:onClick="click1"
        />
    <EditText
        android:id="@+id/edt_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="显示读取的内容"
        />
    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取"
        android:onClick="click2"
        />
</LinearLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
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);
        findViewById(R.id.btn_1);
        findViewById(R.id.btn_2);
    }
    public void click1(View view){
        EditText et1=findViewById(R.id.edt_1);
        String fileName="data.txt";
        String content=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();
            }
        }
        Toast.makeText(MainActivity.this,"储存成功",Toast.LENGTH_LONG).show();

    }
    public void click2(View v){
        EditText et2=findViewById(R.id.edt_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();
            }
        }
        et2.setText(content);

    }


}

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:id="@+id/tv_1"
            />
        <EditText
            android:id="@+id/edt_1"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:id="@+id/tv_2"
            />
        <EditText
            android:id="@+id/edt_2"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="50dp"
        >
        <CheckBox
            android:id="@+id/cb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            />
        <CheckBox
            android:id="@+id/cb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动登录"
            />
        <Button
            android:id="@+id/bt_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            />
    </LinearLayout>
</LinearLayout>
package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;


public class Save {
    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;
    }

    public static boolean saveUserInfo(Context context, String account, String password) {
        SharedPreferences sp=context.getSharedPreferences("data",context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("Username",account);
        editor.putString("pwd",password);
        editor.commit();
        return true;
    }
}
package com.example.myapplication;

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_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=Save.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_1);
        et_password=findViewById(R.id.et_2);
        btn_login=findViewById(R.id.btn_1);
        btn_login.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_account.getText().toString().trim();
                String password=et_password.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;
                }
                Toast.makeText(this,"登录成功",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()){
            case R.id.cb_1:
                String account=et_account.getText().toString().trim();
                String password=et_password.getText().toString();
                boolean isSaveSuccess=Save.saveUserInfo(this,account,password);
                if(isSaveSuccess&&b){
                    Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(this,"保存失败",Toast.LENGTH_LONG).show();
                    et_account.setText(null);
                    et_password.setText(null);
                }
                break;
            case R.id.cb_2:
                account=et_account.getText().toString().trim();
                password=et_password.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();
                }
        }
    }}

 

posted @ 2021-11-07 23:33  于国庆  阅读(24)  评论(0编辑  收藏  举报