Android第六次作业
阅读原文时间:2023年07月09日阅读:1

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


<TextView  
    android:id="@+id/tv\_1"  
    android:layout\_width="match\_parent"  
    android:layout\_height="wrap\_content"  
    android:layout\_marginBottom="5dp"  
    android:background="#000000"  
    android:padding="10dp"  
    android:text="内部存储空间文件操作"  
    android:textColor="#ffffff"  
    android:textSize="25sp" />

<EditText  
    android:id="@+id/et\_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:background="#FFFFFF"  
    android:onClick="click1"  
    android:text="写入"  
    android:textColor="#ffffff" />

<EditText  
    android:id="@+id/et\_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:background="#2C2A2A"  
    android:onClick="click2"  
    android:text="读取"  
    android:textColor="#ffffff" />

package com.example.myapplication1;

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);  
}  
public void click1(View view) {  
    String filename = "data.txt";  
    String content=((EditText)findViewById(R.id.et\_1)).getText().toString();  
    FileOutputStream fos = null;  
    try {  
        fos = openFileOutput(filename, MODE\_PRIVATE);  
        fos.write(content.getBytes());  
    } catch (Exception e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    } finally {  
        if (fos != null)  
            try {  
                fos.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
    }  
    Toast.makeText(this, "保存成功", 0).show();  
}

public void click2(View view) {  
    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) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    } finally {  
        if (fis != null)  
            try {  
                fis.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
    }  
    ((EditText) findViewById(R.id.et\_2)).setText(content);

}

}

图片二 使用sharedpreference实现记住密码功能


<TextView  
    android:id="@+id/tv1"  
    android:layout\_width="match\_parent"  
    android:layout\_height="wrap\_content"  
    android:layout\_marginBottom="15dp"  
    android:background="#27D52C"  
    android:padding="5dp"  
    android:text="用户登录"  
    android:textColor="#FFFFFF"  
    android:textSize="25sp" />

<LinearLayout  
    android:id="@+id/ll\_1"  
    android:layout\_width="match\_parent"  
    android:layout\_height="50dp"  
    android:layout\_margin="10dp"  
    android:orientation="horizontal">

    <TextView  
        android:id="@+id/tv2"  
        android:layout\_width="wrap\_content"  
        android:layout\_height="wrap\_content"  
        android:layout\_margin="10dp"  
        android:text="账号"  
        android:textColor="#000000"  
        android:textSize="18sp" />

    <EditText  
        android:id="@+id/et1"  
        android:layout\_width="match\_parent"  
        android:layout\_height="wrap\_content"  
        android:layout\_marginTop="10dp"  
        android:hint="请输入账号" />

</LinearLayout>

<LinearLayout  
    android:id="@+id/ll\_2"  
    android:layout\_width="match\_parent"  
    android:layout\_height="50dp"  
    android:layout\_margin="10dp"  
    android:orientation="horizontal">

    <TextView  
        android:id="@+id/tv3"  
        android:layout\_width="wrap\_content"  
        android:layout\_height="wrap\_content"  
        android:layout\_margin="10dp"  
        android:text="密码"  
        android:textColor="#000000"  
        android:textSize="18sp" />

    <EditText  
        android:id="@+id/et2"  
        android:layout\_width="match\_parent"  
        android:layout\_height="wrap\_content"  
        android:layout\_marginTop="10dp"  
        android:hint="请输入密码" />

</LinearLayout>

<LinearLayout  
    android:id="@+id/ll\_3"  
    android:layout\_width="match\_parent"  
    android:layout\_height="80dp"  
    android:layout\_marginLeft="25dp"  
    android:orientation="horizontal">

    <CheckBox  
        android:id="@+id/cb1"  
        android:layout\_width="wrap\_content"  
        android:layout\_height="wrap\_content"  
        android:layout\_margin="10dp"  
        android:text="记住密码" />

    <CheckBox  
        android:id="@+id/cb2"  
        android:layout\_width="wrap\_content"  
        android:layout\_height="wrap\_content"  
        android:layout\_margin="10dp"  
        android:text="自动登录" />

    <Button  
        android:id="@+id/btn1"  
        android:layout\_width="wrap\_content"  
        android:layout\_height="wrap\_content"  
        android:layout\_margin="10dp"  
        android:text="登录" />

</LinearLayout>

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 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"));

    }  
}

private void initView() {  
    et\_account = (EditText) findViewById(R.id.et1);  
    et\_password = (EditText) findViewById(R.id.et2);  
    btn\_login = (Button) findViewById(R.id.btn1);  
    btn\_login.setOnClickListener(this);  
    CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);  
    cb1.setOnCheckedChangeListener(this);  
    CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);  
    cb2.setOnCheckedChangeListener(this);  
}

@Override  
public void onClick(View v) {  
    switch(v.getId()){  
        case R.id.btn1:  
            String account=et\_account.getText().toString().trim();  
            String password=et\_password.getText().toString();  
            if(TextUtils.isEmpty(account)){  
                Toast.makeText(this,"输入QQ账号",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.cb1 :  
            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.cb2: 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.myapplication;

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

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

public class saveQQ {
public static boolean SaveUserInfo(Context context, String account, String password) {
SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("account", account);
edit.putString("pwd", password);
edit.commit();
return true;
}
public static MapgetUserInfo(Context context){
SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
String account=sp.getString("account",null);
String password=sp.getString("pwd",null);
MapuserMap=new HashMap();
userMap.put("account",account);
userMap.put("password",password);
return userMap;
}

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器