Android第十一,十二周作业
图片一 用内部存储实现文件写入和读取功能
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity" 8 android:orientation="vertical"> 9 10 <EditText 11 android:id="@+id/edt_1" 12 android:layout_width="600px" 13 android:layout_height="wrap_content" 14 android:textSize="18sp"/> 15 16 <Button 17 android:id="@+id/btn_1" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="写入" 21 android:onClick="click1"/> 22 23 <TextView 24 android:id="@+id/tv_1" 25 android:layout_width="600px" 26 android:layout_height="wrap_content" 27 android:textSize="24sp"/> 28 29 <Button 30 android:id="@+id/btn_2" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:text="读取" 34 android:onClick="click2"/> 35 36 </LinearLayout>
1 package com.example.hw1; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 import android.widget.Toast; 10 11 import java.io.FileInputStream; 12 import java.io.FileOutputStream; 13 import java.io.IOException; 14 15 public class MainActivity extends AppCompatActivity { 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 } 22 public void click1(View view){ 23 String filename = "data.txt"; 24 EditText edt = findViewById(R.id.edt_1); 25 String content = edt.getText().toString(); 26 FileOutputStream fos = null; 27 try { 28 fos = openFileOutput(filename, MODE_PRIVATE); 29 fos.write(content.getBytes()); 30 }catch (Exception e){ 31 e.printStackTrace(); 32 }finally { 33 try { 34 if (fos != null){ 35 fos.close(); 36 } 37 }catch (IOException e){ 38 e.printStackTrace(); 39 } 40 } 41 Toast.makeText(this,content,1).show(); 42 } 43 44 public void click2(View view){ 45 TextView tv = findViewById(R.id.tv_1); 46 String content = ""; 47 FileInputStream fis = null; 48 try { 49 fis = openFileInput("data.txt"); 50 byte[] buffer = new byte[fis.available()]; 51 fis.read(buffer); 52 content = new String(buffer); 53 }catch (Exception e) { 54 e.printStackTrace(); 55 }finally { 56 try { 57 if (fis != null) { 58 fis.close(); 59 } 60 }catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64 tv.setText(content); 65 } 66 }
图片二 使用sharedpreference实现记住密码功能
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".Main2Activity" 8 android:orientation="vertical"> 9 10 <EditText 11 android:id="@+id/edt_2" 12 android:layout_width="300dp" 13 android:layout_height="wrap_content" 14 android:text="账号:" /> 15 16 <EditText 17 android:id="@+id/edt_3" 18 android:layout_width="300dp" 19 android:layout_height="wrap_content" 20 android:text="密码:"/> 21 22 <Button 23 android:id="@+id/btn_3" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="登录"/> 27 28 </LinearLayout>
1 package com.example.hw1; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Context; 6 import android.content.SharedPreferences; 7 import android.os.Bundle; 8 import android.text.TextUtils; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.EditText; 12 import android.widget.Toast; 13 14 import java.util.HashMap; 15 import java.util.Map; 16 17 public class Main2Activity extends AppCompatActivity implements View.OnClickListener { 18 public EditText edt_2; 19 public EditText edt_3; 20 public Button btn_3; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main2); 25 initView(); 26 Map<String, String> userInfo = this.get(this); 27 if (userInfo != null){ 28 edt_2.setText(userInfo.get("account")); 29 edt_3.setText(userInfo.get("password")); 30 } 31 } 32 33 34 public static boolean save(Context context, String account, String password){ 35 SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE); 36 SharedPreferences.Editor edit = sp.edit(); 37 edit.putString("username",account); 38 edit.putString("pwd",password); 39 edit.commit(); 40 return true; 41 } 42 43 public static Map<String,String> get(Context context){ 44 SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE); 45 String account = sp.getString("username",null); 46 String password = sp.getString("pwd",null); 47 Map<String, String> userMap = new HashMap<String, String>(); 48 userMap.put("account", account); 49 userMap.put("password", password); 50 return userMap; 51 } 52 53 public void initView(){ 54 edt_2 = (EditText) findViewById(R.id.edt_2); 55 edt_3 = (EditText) findViewById(R.id.edt_3); 56 btn_3 = (Button) findViewById(R.id.btn_3); 57 btn_3.setOnClickListener(this); 58 } 59 60 @Override 61 public void onClick(View view) { 62 switch (view.getId()){ 63 case R.id.btn_3: 64 String account = edt_2.getText().toString().trim(); 65 String password = edt_3.getText().toString(); 66 if (TextUtils.isEmpty(account)){ 67 Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show(); 68 return; 69 } 70 if (TextUtils.isEmpty(password)){ 71 Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show(); 72 return; 73 } 74 Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show(); 75 boolean isSaveSuccess = save(this,account,password); 76 if (isSaveSuccess){ 77 Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); 78 }else { 79 Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show(); 80 } 81 break; 82 } 83 } 84 }