作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来
activity_practice2的layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 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 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.PractiseActivity2" 11 android:orientation="vertical"> 12 13 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:orientation="horizontal"> 17 <TextView 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="用户名:"/> 21 <EditText 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:id="@+id/et_username"/> 25 </LinearLayout> 26 <LinearLayout 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:orientation="horizontal"> 30 <TextView 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:text="密码:"/> 34 <EditText 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:id="@+id/et_password" 38 android:inputType="textPassword"/> 39 </LinearLayout> 40 <LinearLayout 41 android:layout_width="match_parent" 42 android:layout_height="wrap_content" 43 android:orientation="horizontal"> 44 <Button 45 android:layout_width="0dp" 46 android:layout_height="wrap_content" 47 android:text="登陆" 48 android:onClick="bt_dl_onClick" 49 android:layout_weight="1"/> 50 <Button 51 android:layout_width="0dp" 52 android:layout_height="wrap_content" 53 android:text="注册" 54 android:onClick="bt_zc_onClick" 55 android:layout_weight="1"/> 56 </LinearLayout> 57 58 <!--<Button--> 59 <!--android:layout_width="match_parent"--> 60 <!--android:layout_height="wrap_content"--> 61 <!--android:text="fdasf"--> 62 <!--android:onClick="bt_onClick"--> 63 <!--android:id="@+id/bt_1"/>--> 64 </LinearLayout>
activity_practice2_2的layout文件:
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 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.PractiseActivity2_2"> 11 12 <EditText 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:hint="用户名称" 16 android:id="@+id/et_username_1"/> 17 <EditText 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:hint="登陆密码" 21 android:id="@+id/et_password_1" 22 android:layout_below="@id/et_username_1" 23 android:inputType="textPassword"/> 24 <EditText 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:hint="个性签名" 28 android:id="@+id/et_gxqm" 29 android:layout_below="@id/et_password_1"/> 30 31 <LinearLayout 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:layout_alignParentBottom="true"> 35 <Button 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="确定" 39 android:layout_weight="1" 40 android:onClick="bt_qd_onClick"/> 41 <Button 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:text="取消" 45 android:layout_weight="1" 46 android:onClick="bt_qx_onClick"/> 47 </LinearLayout> 48 <!--<Button--> 49 <!--android:layout_width="match_parent"--> 50 <!--android:layout_height="wrap_content"--> 51 <!--android:text="fdasf"--> 52 <!--android:onClick="bt_onClick"--> 53 <!--android:id="@+id/bt_1"--> 54 <!--android:layout_below="@+id/et_gxqm"/>--> 55 </RelativeLayout>
activity_practice2_3的layout文件:
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 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.PractiseActivity2_3"> 11 12 <TextView 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:id="@+id/tv_username" 16 android:textSize="30dp"/> 17 <TextView 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:id="@+id/tv_gxqm" 21 android:layout_below="@id/tv_username" 22 android:textSize="30dp"/> 23 </RelativeLayout>
PractiseActivity2的java类:
1 package com.hanqi.testapp3; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.Toast; 9 10 import java.io.FileInputStream; 11 12 public class PractiseActivity2 extends AppCompatActivity { 13 14 EditText et_username; 15 EditText et_password; 16 17 // Button bt_1; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_practise2); 22 et_username = (EditText)findViewById(R.id.et_username); 23 et_password = (EditText)findViewById(R.id.et_password); 24 //bt_1 = (Button)findViewById(R.id.bt_1); 25 } 26 public void bt_zc_onClick(View v) 27 { 28 Intent intent = new Intent(this,PractiseActivity2_2.class); 29 startActivity(intent); 30 } 31 32 public void bt_dl_onClick(View v) 33 { 34 String str1 = ""; 35 String str2 = ""; 36 String str3 = ""; 37 String uc = et_username.getText().toString(); 38 String pw = et_password.getText().toString(); 39 40 try { 41 FileInputStream fis = openFileInput("practise2-1.txt"); 42 byte[] b = new byte[1024]; 43 int i; 44 45 while ((i=fis.read(b))>0) 46 { 47 String str = new String(b,0,i); 48 str1 += str; 49 } 50 fis.close(); 51 52 FileInputStream fis2 = openFileInput("practise2-2.txt"); 53 byte[] b2 = new byte[1024]; 54 int i2 = 0; 55 while ((i2=fis2.read(b2))>0) 56 { 57 String str = new String(b2,0,i2); 58 str2 += str; 59 } 60 fis2.close(); 61 62 FileInputStream fis3 = openFileInput("practise2-3.txt"); 63 byte[] b3 = new byte[1024]; 64 int i3 = 0; 65 while ((i3=fis3.read(b3))>0) 66 { 67 String str = new String(b3,0,i3); 68 str3 += str; 69 } 70 fis3.close(); 71 } 72 catch (Exception e) 73 { 74 75 } 76 if (uc.trim().length()==0||pw.trim().length()==0) 77 { 78 Toast.makeText(PractiseActivity2.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show(); 79 return; 80 } 81 if (str1 ==null||(str1!=null&&!str1.equals(uc))) 82 { 83 Toast.makeText(PractiseActivity2.this, "用户未注册", Toast.LENGTH_SHORT).show(); 84 return; 85 } 86 if (!str2.equals(pw)) 87 { 88 Toast.makeText(PractiseActivity2.this, "密码错误", Toast.LENGTH_SHORT).show(); 89 return; 90 } 91 else 92 { 93 Toast.makeText(PractiseActivity2.this, "用户验证成功", Toast.LENGTH_SHORT).show(); 94 Intent intent = new Intent(this,PractiseActivity2_3.class); 95 startActivity(intent); 96 finish(); 97 } 98 } 99 // public void bt_onClick(View v) 100 // { 101 // try { 102 // FileInputStream fis = openFileInput("practise2-1.txt"); 103 // byte[] b = new byte[1024]; 104 // int i; 105 // 106 // while ((i=fis.read(b))>0) 107 // { 108 // String str = new String(b,0,i); 109 // str1 += str; 110 // } 111 // 112 // fis.close(); 113 // } 114 // catch (Exception e) 115 // { 116 // 117 // } 118 // bt_1.setText(str1); 119 // } 120 }
PractiseActivity2_2的java类:
1 package com.hanqi.testapp3; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.Toast; 9 10 import java.io.FileOutputStream; 11 import java.io.PrintStream; 12 13 public class PractiseActivity2_2 extends AppCompatActivity { 14 15 EditText et_username_1; 16 EditText et_password_1; 17 EditText et_gxqm; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_practise2_2); 22 et_username_1 = (EditText)findViewById(R.id.et_username_1); 23 et_password_1 = (EditText)findViewById(R.id.et_password_1); 24 et_gxqm = (EditText)findViewById(R.id.et_gxqm); 25 } 26 public void bt_qd_onClick(View v) 27 { 28 String username = et_username_1.getText().toString(); 29 if (username ==null||username.trim().length()==0) 30 { 31 Toast.makeText(PractiseActivity2_2.this, "请填写用户名", Toast.LENGTH_SHORT).show(); 32 return; 33 } 34 String password = et_password_1.getText().toString(); 35 if (password ==null||password.trim().length()==0) 36 { 37 Toast.makeText(PractiseActivity2_2.this, "请填写密码", Toast.LENGTH_SHORT).show(); 38 return; 39 } 40 String gxqm = et_gxqm.getText().toString(); 41 if (gxqm ==null||gxqm.trim().length()==0) 42 { 43 Toast.makeText(PractiseActivity2_2.this, "个性签名为必填内容", Toast.LENGTH_SHORT).show(); 44 return; 45 } 46 try { 47 FileOutputStream fos = openFileOutput("practise2-1.txt",MODE_PRIVATE); 48 PrintStream ps = new PrintStream(fos); 49 ps.print(username); 50 FileOutputStream fos2 = openFileOutput("practise2-2.txt",MODE_PRIVATE); 51 PrintStream ps2 = new PrintStream(fos2); 52 ps2.print(password); 53 FileOutputStream fos3 = openFileOutput("practise2-3.txt",MODE_PRIVATE); 54 PrintStream ps3 = new PrintStream(fos3); 55 ps3.print(gxqm); 56 ps.close(); 57 ps2.close(); 58 ps3.close(); 59 fos.close(); 60 Toast.makeText(PractiseActivity2_2.this, "保存成功", Toast.LENGTH_SHORT).show(); 61 } 62 catch (Exception e) 63 { 64 65 } 66 Intent intent = new Intent(this,PractiseActivity2.class); 67 startActivity(intent); 68 finish(); 69 } 70 public void bt_qx_onClick(View v) 71 { 72 setResult(RESULT_CANCELED, null); 73 finish(); 74 } 75 76 }
PractiseActivity2_3的java类:
1 package com.hanqi.testapp3; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.TextView; 6 7 import java.io.FileInputStream; 8 9 public class PractiseActivity2_3 extends AppCompatActivity { 10 11 TextView tv_username; 12 TextView tv_gxqm; 13 14 String str1 = ""; 15 String str3 = ""; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_practise2_3); 20 tv_username = (TextView)findViewById(R.id.tv_username); 21 tv_gxqm = (TextView)findViewById(R.id.tv_gxqm); 22 23 try { 24 FileInputStream fis = openFileInput("practise2-1.txt"); 25 byte[] b = new byte[1024]; 26 int i; 27 28 while ((i=fis.read(b))>0) 29 { 30 String str = new String(b,0,i); 31 str1 += str; 32 } 33 34 fis.close(); 35 36 FileInputStream fis3 = openFileInput("practise2-3.txt"); 37 byte[] b3 = new byte[1024]; 38 int i3 = 0; 39 while ((i3=fis3.read(b3))>0) 40 { 41 String str = new String(b3,0,i3); 42 str3 += str; 43 } 44 fis3.close(); 45 } 46 catch (Exception e) 47 { 48 49 } 50 tv_username.setText("用户名是:" + str1); 51 tv_gxqm.setText("个性签名是:"+str3); 52 } 53 }
效果为: