安卓第十一周、十二周作业
图片一 用内部存储实现文件写入和读取功能
<?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:id="@+id/edt_1"
android:layout_width="600px"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入"
android:onClick="click1"/>
<TextView
android:id="@+id/tv_1"
android:layout_width="600px"
android:layout_height="wrap_content"
android:textSize="24sp"/>
<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.hw1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
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";
EditText edt = findViewById(R.id.edt_1);
String content = edt.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(this,content,1).show();
}
public void click2(View view){
TextView tv = findViewById(R.id.tv_1);
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();
}
}
tv.setText(content);
}
}

图片二 使用sharedpreference实现记住密码功能
<?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=".Main2Activity"
android:orientation="vertical">
<EditText
android:id="@+id/edt_2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="账号:" />
<EditText
android:id="@+id/edt_3"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="密码:"/>
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
package com.example.hw1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
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.HashMap;
import java.util.Map;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
public EditText edt_2;
public EditText edt_3;
public Button btn_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
Map<String, String> userInfo = this.get(this);
if (userInfo != null){
edt_2.setText(userInfo.get("account"));
edt_3.setText(userInfo.get("password"));
}
}
public static boolean save(Context context, String account, String password){
SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("username",account);
edit.putString("pwd",password);
edit.commit();
return true;
}
public static Map<String,String> get(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 void initView(){
edt_2 = (EditText) findViewById(R.id.edt_2);
edt_3 = (EditText) findViewById(R.id.edt_3);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_3.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_3:
String account = edt_2.getText().toString().trim();
String password = edt_3.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();
boolean isSaveSuccess = save(this,account,password);
if (isSaveSuccess){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
break;
}
}
}

浙公网安备 33010602011771号