第五次作业

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

<?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:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="请输入内容"
       android:layout_margin="20dp"
       android:id="@+id/et_in"
       />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入"
        android:layout_margin="20dp"
        android:id="@+id/bt_1"
        android:onClick="click1"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="显示输入的内容"
        android:layout_margin="20dp"
        android:id="@+id/et_out"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取"
        android:layout_margin="20dp"
        android:id="@+id/bt_2"
        android:onClick="click2"/>

</LinearLayout>
package com.example;

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.bt_1);
        findViewById(R.id.bt_2);
        findViewById(R.id.et_in);
        findViewById(R.id.et_out);
    }
    public void click1(View view){
        EditText editText1=findViewById(R.id.et_in);
        EditText editText2=findViewById(R.id.et_out);
        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) {
                        e.printStackTrace();
                    }finally {
                        if (fos!=null) {
                            try {
                                fos.close();
                            } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();

    }
    public void click2(View view){
        EditText editText2=findViewById(R.id.et_out);
        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) {
            e.printStackTrace();
        }finally {
            if (fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        editText2.setText(context);
    }
}

 

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/tv_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="账号"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:textSize="15dp"
    />
    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入账号"
        android:layout_toRightOf="@id/tv_1"
        />
    <TextView
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:layout_below="@id/tv_1"
        />

    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:layout_toRightOf="@id/tv_2"
        android:layout_below="@id/et_1"
        />
    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:textSize="20sp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="60dp"
        android:layout_below="@id/tv_2"
        />
    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:textSize="20sp"
        android:layout_toRightOf="@id/cb_1"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="60dp"
        android:layout_below="@id/tv_2"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_2"
        android:layout_centerInParent="true"
        android:text="登录"
        android:layout_marginTop="150dp"
        android:textSize="20dp"

/>
</RelativeLayout>
package com.example;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
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 androidx.annotation.CheckResult;

import static com.example.R.id.tv_2;

public class MainActivity extends Activity 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=saveQQ.getUserInfo(this);
        if(userInfo!=null){
            et_account.setText(userInfo.get("account"));
            et_password.setText(userInfo.get("password"));
        }
    }
    @SuppressLint("WrongViewCast")
    private void initView() {
        // TODO Auto-generated method stub
        et_account=(EditText)findViewById(R.id.et_1);
        et_password=(EditText)findViewById(tv_2);
        btn_login=(Button)findViewById(R.id.btn);
        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 v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
            case R.id.btn:
                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();

        }

    }

    @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=saveQQ.saveUserInfo(this, account, password);
                if(isSaveSuccess&&b){
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).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_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(b)
                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                else{
                    Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
                }

        }
    }
}
package com.example ;

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

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

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



    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-04 18:54  无言De钉钉  阅读(8)  评论(0编辑  收藏  举报