AS第十一、十二周作业
1.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="内部存储空间文件操作" android:textSize="30dp" android:layout_margin="5dp" android:background="#706767"/> <EditText android:id="@+id/et_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="45dp" android:hint="输入你想写入的内容" android:textSize="25dp" android:layout_marginLeft="10dp"/> <Button android:id="@+id/but_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="写入" android:textSize="25dp" android:background="#A09B9B" android:onClick="click1"/> <EditText android:id="@+id/et_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="45dp" android:hint="显示读取的内容" android:textSize="25dp" android:layout_marginLeft="10dp"/> <Button android:id="@+id/but_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="读取" android:textSize="25dp" android:background="#A09B9B" android:onClick="click2"/> </LinearLayout>
package cn.itcast.save; 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){ e.printStackTrace(); }finally{ try { if(fos!=null){ fos.close(); } } catch (Exception e) { e.printStackTrace(); } } Toast.makeText(this, "已写入",Toast.LENGTH_LONG).show(); } public void click2(View view){ EditText editText2=findViewById( R.id.et_2 ); 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 (Exception e) { e.printStackTrace(); } } editText2.setText( content ); } }
2.
package com.example.aaaa; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_name; private EditText et_pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name=(EditText) findViewById(R.id.action_settings); et_pass=(EditText) findViewById(R.id.action_settings); readAccount(); } private void readAccount() { Object info; // TODO Auto-generated method stub File file=new File(getCacheDir(),"1.txt"); if(file.exists()){ try{ FileInputStream fis=new FileInputStream(file); BufferedReader br=new BufferedReader(new InputStreamReader(fis)); String text=br.readLine(); String[] s=text.split("##"); et_name.setText(s[0]); et_pass.setText(s[1]); } catch (Exception e){ e.printStackTrace(); } } } public void login(View v){ String name=et_name.getText().toString(); String pass=et_pass.getText().toString(); CheckBox cb_1=(CheckBox) findViewById(R.id.action_settings); CheckBox cb_2=(CheckBox) findViewById(R.id.action_settings); if(cb_1.isChecked()){ File file=new File(getCacheDir(),"1.txt"); FileOutputStream fos; try{ fos=new FileOutputStream(file); fos.write((name+"##"+pass).getBytes()); fos.close(); } catch (Exception e){ e.printStackTrace(); } } Toast.makeText(this, "登录成功", 0).show(); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:hint="请输入用户名" /> <EditText android:id="@+id/et_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="记住密码" /> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="自动登录" /> </RelativeLayout> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="login" android:text="登录" /> </LinearLayout>