這周沒啥新的學習感覺,由於一直是在用學到的內容作案例。java
上週敲出了一個登陸界面的程序,登陸以前必需要進行註冊,進入註冊界面後能夠進行註冊,將第一次註冊時的用戶名和密碼寫到兩個文檔中,而後註冊完成後會跳轉到登陸界面,而後輸入註冊好的用戶名和密碼,輸入正確後跳轉到應用的界面(一個未作好的九宮格界面)。android
1、Java代碼:app
一、註冊時將用戶名和密碼要寫到兩個文件裏,密碼輸入兩次,若是兩次輸入不一致沒法寫入,用戶名和兩次輸入的密碼若是有未填寫的也沒法寫入,沒法註冊。ide
二、登陸時定義兩個字符串分別是讀入以前寫入的用戶名和密碼,而後和用戶輸入的用戶名、密碼判斷是否相同,不相同沒法登陸。學習
三、修改密碼放在了九宮格界面中,如要修改密碼在該界面點擊修改密碼後跳轉到修改密碼的界面,只是修改的密碼與以前註冊的密碼不在一個文件,即修改密碼不會改變註冊的密碼。this
四、忘記密碼還未設計具體的代碼,由於具體的邏輯還沒搞清楚。spa
2、具體代碼以下:設計
一、註冊代碼code
1 package cn.itcast.activity_login; 2 3 import java.io.FileOutputStream; 4 import cn.itcast.activity_login.R; 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.content.Context; 8 import android.content.Intent; 9 import android.text.TextUtils; 10 import android.view.Menu; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 import android.widget.EditText; 15 import android.widget.Toast; 16 17 public class MainActivity_register extends Activity { 18 private EditText et_rUN; 19 private EditText et_rUNP; 20 private EditText et_againRUNP; 21 private Button btn_register; 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main_activity_register); 26 et_rUN=(EditText)findViewById(R.id.et_rUN); 27 et_rUNP=(EditText)findViewById(R.id.et_rUNP); 28 et_againRUNP=(EditText)findViewById(R.id.et_againRUNP); 29 btn_register=(Button)findViewById(R.id.btn_register); 30 btn_register.setOnClickListener(new MyOnClicklistener()); 31 } 32 private class MyOnClicklistener implements OnClickListener 33 { 34 35 @Override 36 public void onClick(View view) { 37 String saveRUN=et_rUN.getText().toString().trim(); 38 String saveRUNP=et_rUNP.getText().toString().trim(); 39 String saveAgainRUNP=et_againRUNP.getText().toString().trim(); 40 // TODO Auto-generated method stub 41 switch(view.getId()) 42 { 43 case R.id.btn_register: 44 if(TextUtils.isEmpty(saveRUN)||TextUtils.isEmpty(saveRUNP)||TextUtils.isEmpty(saveAgainRUNP)) 45 { 46 Toast.makeText(MainActivity_register.this, "您有未輸入的信息!",Toast.LENGTH_SHORT).show(); 47 } 48 else 49 { 50 if(saveRUNP.equals(saveAgainRUNP)) 51 { 52 FileOutputStream fosSaveRUN; 53 FileOutputStream fosSaveRUNP; 54 try 55 { 56 fosSaveRUN=openFileOutput("saveRUN.txt",Context.MODE_APPEND); 57 fosSaveRUN.write(saveRUN.getBytes()); 58 fosSaveRUN.close(); 59 fosSaveRUNP=openFileOutput("saveRUNP.txt",Context.MODE_APPEND); 60 fosSaveRUNP.write(saveRUNP.getBytes()); 61 fosSaveRUNP.close(); 62 } 63 catch(Exception e) 64 { 65 e.printStackTrace(); 66 } 67 Toast.makeText(MainActivity_register.this, "已成功註冊!\n用戶名:"+saveRUN+"\n密碼:"+saveRUNP,Toast.LENGTH_SHORT).show(); 68 Intent intent=new Intent(MainActivity_register.this,MainActivity_login.class); 69 startActivity(intent); 70 } 71 else 72 { 73 Toast.makeText(MainActivity_register.this, "兩次輸入的密碼不一致!",Toast.LENGTH_SHORT).show(); 74 } 75 } 76 break; 77 } 78 79 } 80 } 81 82 @Override 83 public boolean onCreateOptionsMenu(Menu menu) { 84 // Inflate the menu; this adds items to the action bar if it is present. 85 getMenuInflater().inflate(R.menu.main_activity_register, menu); 86 return true; 87 } 88 89 }
二、登陸代碼blog
1 package cn.itcast.activity_login; 2 3 4 import java.io.FileInputStream; 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.text.TextUtils; 9 import android.util.Log; 10 import android.view.Menu; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 import android.widget.CheckBox; 15 import android.widget.EditText; 16 import android.widget.Toast; 17 18 public class MainActivity_login extends Activity { 19 private EditText et_un; 20 private EditText et_upw; 21 private CheckBox ck_rupw; 22 private Button bt_login; 23 private Button bt_forget; 24 private Button bt_register; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main_activity_login); 30 et_un = (EditText) findViewById(R.id.et_un); 31 et_upw = (EditText) findViewById(R.id.et_upw); 32 ck_rupw = (CheckBox) findViewById(R.id.ck_rupw); 33 bt_login = (Button) findViewById(R.id.bt_login); 34 bt_forget = (Button) findViewById(R.id.bt_forget); 35 bt_register=(Button) findViewById(R.id.bt_register); 36 bt_login.setOnClickListener(new MyOnClickListener()); 37 bt_forget.setOnClickListener(new MyOnClickListener()); 38 bt_register.setOnClickListener(new MyOnClickListener()); 39 } 40 41 42 @Override 43 public boolean onCreateOptionsMenu(Menu menu) { 44 // Inflate the menu; this adds items to the action bar if it is present. 45 getMenuInflater().inflate(R.menu.main_activity_login, menu); 46 return true; 47 } 48 private class MyOnClickListener implements OnClickListener{ 49 50 @Override 51 public void onClick(View view) { 52 String username=et_un.getText().toString(); 53 String password=et_upw.getText().toString(); 54 // TODO Auto-generated method stub 55 switch(view.getId()){ 56 case R.id.bt_login: 57 if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){ 58 Toast.makeText(MainActivity_login.this, "用戶名或密碼不能爲空",Toast.LENGTH_SHORT).show(); 59 }else{ 60 String readRUN=""; 61 String readRUNP=""; 62 try 63 { 64 FileInputStream fisReadRUN=openFileInput("saveRUN.txt"); 65 byte[] bufferRUN=new byte[fisReadRUN.available()]; 66 fisReadRUN.read(bufferRUN); 67 readRUN=new String(bufferRUN); 68 fisReadRUN.close(); 69 //Toast.makeText(MainActivity_login.this, "用戶名"+readRUN,Toast.LENGTH_SHORT).show(); 70 FileInputStream fisReadRUNP=openFileInput("saveRUNP.txt"); 71 byte[] bufferRUNP=new byte[fisReadRUNP.available()]; 72 fisReadRUNP.read(bufferRUNP); 73 readRUNP=new String(bufferRUNP); 74 fisReadRUNP.close(); 75 //Toast.makeText(MainActivity_login.this, "密碼"+readRUNP,Toast.LENGTH_SHORT).show(); 76 } 77 catch(Exception e) 78 { 79 e.printStackTrace(); 80 } 81 if(username.equals(readRUN)&&password.equals(readRUNP)) 82 { 83 if(ck_rupw.isChecked()){ 84 Log.i("用戶名爲:"+username,"密碼爲:"+password); 85 } 86 else{ 87 Log.i("不用記住用戶名", "不用記住密碼"); 88 } 89 Intent intentL=new Intent(MainActivity_login.this,MainActivity_logined.class); 90 startActivity(intentL); 91 } 92 else 93 { 94 Toast.makeText(MainActivity_login.this, "用戶名或密碼輸入錯誤",Toast.LENGTH_SHORT).show(); 95 } 96 } 97 break; 98 case R.id.bt_forget: 99 Intent intentC=new Intent(MainActivity_login.this,MainActivity_forget.class); 100 startActivity(intentC); 101 break; 102 case R.id.bt_register: 103 Intent intentR=new Intent(MainActivity_login.this,MainActivity_register.class); 104 startActivity(intentR); 105 break; 106 } 107 108 } 109 } 110 111 /*public void click(View view){ 112 Intent intent=new Intent(this,MainActivity_logined.class); 113 startActivity(intent); 114 }*/ 115 }
三、修改密碼
1 package cn.itcast.activity_login; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.text.TextUtils; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.EditText; 12 import android.widget.Toast; 13 14 public class MainActivity_changePW extends Activity { 15 private EditText et_before; 16 private EditText et_after; 17 private EditText et_again; 18 private Button bt_submit; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main_activity_change_pw); 23 et_before=(EditText)findViewById(R.id.et_before); 24 et_after=(EditText)findViewById(R.id.et_after); 25 et_again=(EditText)findViewById(R.id.et_again); 26 bt_submit=(Button)findViewById(R.id.bt_submit); 27 bt_submit.setOnClickListener(new MyOnClickListener()); 28 } 29 private class MyOnClickListener implements OnClickListener 30 { 31 32 @Override 33 public void onClick(View v) { 34 String before=et_before.getText().toString(); 35 String after=et_after.getText().toString(); 36 String again=et_again.getText().toString(); 37 // TODO Auto-generated method stub 38 switch(v.getId()) 39 { 40 case R.id.bt_submit: 41 if(TextUtils.isEmpty(before)||TextUtils.isEmpty(after)||TextUtils.isEmpty(again)) 42 { 43 Toast.makeText(MainActivity_changePW.this, "您有未輸入的密碼!",Toast.LENGTH_SHORT).show(); 44 } 45 else 46 { 47 if(after.equals(again)) 48 { 49 //Toast.makeText(MainActivity_changePW.this, "密碼已成功修改!\n"+after+"\n"+again,Toast.LENGTH_SHORT).show(); 50 Toast.makeText(MainActivity_changePW.this, "密碼已成功修改!",Toast.LENGTH_SHORT).show(); 51 Intent intent1=new Intent(MainActivity_changePW.this,MainActivity_logined.class); 52 startActivity(intent1); 53 } 54 else 55 { 56 Toast.makeText(MainActivity_changePW.this, "修改密碼兩次輸入不一致!",Toast.LENGTH_SHORT).show(); 57 } 58 } 59 break; 60 } 61 } 62 63 } 64 65 @Override 66 public boolean onCreateOptionsMenu(Menu menu) { 67 // Inflate the menu; this adds items to the action bar if it is present. 68 getMenuInflater().inflate(R.menu.main_activity_change_pw, menu); 69 return true; 70 } 71 72 }
3、存在的問題
一、用戶名和密碼寫入時應該是能夠重寫,而不是追加,否側沒法進行修改密碼;
二、每一次註冊時不可能只寫到一個文件中,應該是說每次註冊時會寫到不一樣的文件裏,這樣每一個用戶的資料就分開了,這樣也方便以後查找用戶資料;
三、註冊時若用戶名相同沒法註冊成功,這就要涉及全部用戶資料的遍歷,這也是之後要改進的地方;
四、修改密碼時應該已經能夠對用戶資料進行操做,如何獲取到該用戶的資料的代碼還沒想出來;