Android第11周作业
1.
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity"> 7 8 <EditText 9 android:id="@+id/et_1" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_marginTop="25dp" 13 android:hint="输入你想写入的内容" /> 14 15 <Button 16 android:id="@+id/btn_1" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_below="@+id/et_1" 20 android:onClick="click1" 21 android:text="写入"/> 22 23 <EditText 24 android:id="@+id/et_2" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:layout_below="@+id/btn_1" 28 android:hint="显示读取内容" /> 29 30 <Button 31 android:id="@+id/btn_2" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:layout_below="@+id/et_2" 35 android:onClick="click2" 36 android:text="读取"/> 37 </RelativeLayout>
1 package com.example.week11; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.view.View; 9 import android.widget.EditText; 10 import android.widget.Toast; 11 12 public class MainActivity extends Activity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 } 19 20 public void click1(View view) { 21 EditText editText1 = findViewById(R.id.et_1); 22 String filename = "data.txt"; 23 String context = editText1.getText().toString(); 24 FileOutputStream fos = null; 25 try { 26 fos = openFileOutput(filename, MODE_PRIVATE); 27 fos.write(context.getBytes()); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } finally { 31 if (fos != null) { 32 try { 33 fos.close(); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 } 39 Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); 40 41 } 42 43 public void click2(View view) { 44 EditText editText2 = findViewById(R.id.et_2); 45 String context = ""; 46 FileInputStream fis = null; 47 try { 48 fis = openFileInput("data.txt"); 49 byte[] buffer = new byte[fis.available()]; 50 fis.read(buffer); 51 context = new String(buffer); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } finally { 55 if (fis != null) { 56 try { 57 fis.close(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 } 62 } 63 editText2.setText(context); 64 } 65 }
2.
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".Main2Activity"> 7 8 <TextView 9 android:id="@+id/tv_2" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="账号" 13 android:layout_marginLeft="10dp" 14 android:textSize="25dp" 15 android:layout_marginTop="70dp"/> 16 <TextView 17 android:id="@+id/tv_3" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="密码" 21 android:layout_marginLeft="10dp" 22 android:textSize="25dp" 23 android:layout_marginTop="120dp"/> 24 <EditText 25 android:id="@+id/et_1" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:hint="请输入用户名" 29 android:textSize="25dp" 30 android:layout_toRightOf="@id/tv_2" 31 android:layout_marginTop="60dp"/> 32 <EditText 33 android:id="@+id/et_2" 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" 36 android:hint="请输入密码" 37 android:textSize="25dp" 38 android:layout_toRightOf="@id/tv_3" 39 android:layout_marginTop="110dp"/> 40 <CheckBox 41 android:id="@+id/cb_1" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:textSize="25dp" 45 android:text="记住密码" 46 android:layout_marginTop="190dp" 47 android:layout_marginLeft="20dp"/> 48 <CheckBox 49 android:id="@+id/cb_2" 50 android:layout_width="wrap_content" 51 android:layout_height="wrap_content" 52 android:textSize="25dp" 53 android:text="自动登录" 54 android:layout_marginTop="190dp" 55 android:layout_marginLeft="160dp"/> 56 <Button 57 android:id="@+id/but_1" 58 android:layout_width="wrap_content" 59 android:layout_height="wrap_content" 60 android:layout_marginTop="270dp" 61 android:layout_marginLeft="150dp" 62 android:textSize="25dp" 63 android:text="登录" 64 android:background="#706767" 65 android:onClick="click1"/> 66 67 </RelativeLayout>
1 package com.example.week11; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 import android.content.SharedPreferences; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.CheckBox; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 public class Main2Activity extends AppCompatActivity { 12 private EditText userName,passWord; 13 private CheckBox box; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate( savedInstanceState ); 18 setContentView( R.layout.activity_main2 ); 19 userName=findViewById( R.id.et_1); 20 passWord=findViewById( R.id.et_2); 21 box=findViewById( R.id.cb_1 ); 22 load(); 23 } 24 public void click1(View view){ 25 String name=userName.getText().toString(); 26 String pwd=passWord.getText().toString(); 27 if (box.isChecked()){ 28 SharedPreferences sp=getSharedPreferences( "info",MODE_PRIVATE ); 29 SharedPreferences.Editor et=sp.edit(); 30 et.putString( "NAME",name); 31 et.putString( "PWD",pwd); 32 et.commit(); 33 Toast.makeText( this,"登陆成功",Toast.LENGTH_LONG ).show(); 34 35 }else { 36 Toast.makeText( this,"登陆成功",Toast.LENGTH_LONG ).show(); 37 } 38 } 39 40 private void load() { 41 SharedPreferences sp=getSharedPreferences( "info",MODE_PRIVATE ); 42 String nameString=sp.getString( "NAME","" ); 43 String pwdString=sp.getString( "PWD","" ); 44 userName.setText( nameString ); 45 passWord.setText( pwdString ); 46 } 47 48 }