十一,十二周作业(数据存储,QQ实现记住密码)

 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;
        }
    }
}

 

 

    

 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>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:layout_margin="10dp">

<CheckBox
android:id="@+id/cb1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
<CheckBox
android:id="@+id/cb2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"/>
<Button
android:layout_weight="1"
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="登录" />

</LinearLayout>

</LinearLayout>
 

逻辑代码

  工具类:


package com.example.hp.saveqq;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.CheckBox;
import android.widget.EditText;
import java.util.HashMap;
import java.util.Map;

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

public class FileSaveQQ {
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
//存储数据
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.apply();
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;
}

public static void clear(){
editor.clear();
editor.apply();
}

public static void remember(Context context, EditText et1, EditText et2, CheckBox cb1){
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
boolean pang=sharedPreferences.getBoolean("data",false);
if(pang)
{
et1.setText(sharedPreferences.getString("Name",""));
et2.setText(sharedPreferences.getString("Password",""));
cb1.setChecked(true);
}
}
}
 

  界面交互


package com.example.hp.saveqq;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
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.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

import static android.content.SharedPreferences.*;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText et1;
private EditText et2;
private Button bt;
private CheckBox cb1;
private String name;
private String password;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1=(CheckBox)findViewById(R.id.cb1);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(this);
name=et1.getText().toString();
password=et2.getText().toString();
FileSaveQQ.remember(this,et1,et2,cb1);
}

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

}

}
}




}
 

 

 

posted @ 2021-10-26 23:16  CYP-Bk  阅读(104)  评论(0)    收藏  举报