AndroidStudio 3.1.4

1.界面由5個TextView和3個TextEdit還有2個Button組成

2.按鈕的按下事件分別為reg和del,各組件ID如下圖

3.項目名為:Register 布局界面文件名稱為activity_register.xml

4.代碼如下:
首先,定義一個函數 thisif 函數名可以自擬,用於判斷輸入的內容是否合法
1 public boolean thisif(){ 2 3 //創建三個信息框留作備用 4 AlertDialog.Builder message1 = new AlertDialog.Builder(this); 5 message1.setMessage("密碼或確認密碼不能為空!"); 6 message1.setPositiveButton("確定",click1); 7 AlertDialog me1 = message1.create(); 8 9 AlertDialog.Builder message2 = new AlertDialog.Builder(this); 10 message2.setMessage("賬號不能為空!"); 11 message2.setPositiveButton("確定",click1); 12 AlertDialog me2 = message2.create(); 13 14 AlertDialog.Builder message3 = new AlertDialog.Builder(this); 15 message3.setMessage("兩次輸入密碼不匹配!"); 16 message3.setPositiveButton("確定",click1); 17 AlertDialog me3 = message3.create(); 18 19 //獲取三個TextEdit的內容 20 EditText et = (EditText)findViewById(R.id.userEdit); 21 String user = et.getText().toString(); 22 23 EditText password1 = (EditText)findViewById(R.id.EditPW1); 24 String pw1 = password1.getText().toString(); 25 26 EditText password2 = (EditText)findViewById(R.id.EditPW2); 27 String pw2 = password2.getText().toString(); 28 29 //首先判斷密碼編輯框是否輸入內容 30 if (pw1 == null || pw1.length() == 0 || pw2 == null || pw2.length() == 0){ 31 //如果沒有輸入內容則彈出信息框1 32 me1.show(); 33 return false; 34 }else if (user == null || user.length() == 0){ 35 //如果賬號沒有輸入內容則彈出信息框2 36 me2.show(); 37 return false; 38 } 39 40 if (pw1.equals(pw2)){ 41 //驗證完成返回真 42 return true; 43 }else { 44 //如果兩次輸入的密碼不同則彈出信息框3 45 me3.show(); 46 return false; 47 } 48 }
新建一個函數 thisfileexist 函數名可以自擬,用於驗證文件是否存在,1個參數,參數為文件名
1 public boolean thisfileexist(String file){ 2 //定義數組f1 3 String[] f1; 4 //將獲取到的私有文件列表保存到數組f1 5 f1 = fileList(); 6 7 int i; 8 //依次抽取數組成員與要創建的文件名進行比對 9 for (i = 0 ; i < f1.length ; i++){ 10 if (f1[i].equals(file)){ 11 //如果一樣則代表文件存在,返回真 12 return true; 13 } 14 } 15 //循環完成依舊沒有匹配項,則代表文件不存在,返回假 16 return false; 17 }
需要一個函數寫文件,函數名為 thiswrite 參數2個 參數一為欲寫入的數據,參數二為文件名
1 public void thiswrite(String data,String fileName){ 2 AlertDialog.Builder message = new AlertDialog.Builder(this); 3 message.setPositiveButton("確定",click1); 4 5 //驗證文件是否已經存在 6 if (thisfileexist(fileName)){ 7 8 message.setMessage("用戶名已被注冊!"); 9 AlertDialog me = message.create(); 10 me.show(); 11 12 return; 13 } 14 15 //寫到私有文件目錄 16 17 try { 18 //私有文件寫入方法openFileOutput,屬性為僅允許本程序更改 MODE_PRIVATE 19 FileOutputStream fout = openFileOutput(fileName,MODE_PRIVATE); 20 //數據類型轉換 21 byte[] bytes = data.getBytes(); 22 //寫到文件 23 fout.write(bytes); 24 fout.close(); 25 26 message.setMessage("注冊賬號成功!"); 27 AlertDialog m1 = message.create(); 28 m1.show(); 29 30 //System.out.println("創建成功!"); 31 32 }catch (IOException e){ 33 e.printStackTrace(); 34 35 message.setMessage("寫入文件失敗!"); 36 AlertDialog m2 = message.create(); 37 m2.show(); 38 39 //System.out.println("創建失敗!"); 40 } 41 42 }
函數 thisdel 用於清除之前保存的賬號
1 public void thisdel(){ 2 AlertDialog.Builder message = new AlertDialog.Builder(this); 3 message.setPositiveButton("確定",click1); 4 5 //獲取所有私有目錄文件 6 String[] file; 7 file = fileList(); 8 int i; 9 for (i = 0 ; i < file.length ; i++ ){ 10 //循環刪除每一個文件 11 System.out.println(file[i]); 12 deleteFile(file[i]); 13 } 14 message.setMessage("緩存刪除完成!"); 15 AlertDialog M1 = message.create(); 16 M1.show(); 17 }
補充一個彈出的信息框的按鈕按下接收的函數
1 private DialogInterface.OnClickListener click1 = new DialogInterface.OnClickListener() { 2 @Override 3 public void onClick(DialogInterface dialog, int which) { 4 dialog.cancel(); 5 } 6 };
當注冊按鈕被按下時執行的函數
1 public void reg(View view){ 2 //驗證用戶輸入的信息是否合法 3 if (thisif() == true){ 4 //合法則獲取用戶輸入的內容 5 EditText user = (EditText)findViewById(R.id.userEdit); 6 EditText pw = (EditText)findViewById(R.id.EditPW1); 7 8 String data = pw.getText().toString(); 9 String file = user.getText().toString() + ".txt"; 10 //對獲取的內容進行保存 11 thiswrite(data,file); 12 13 //這一句是調試輸出的 14 System.out.println("true"); 15 16 }else{ 17 18 //如果不合法則不保存數據 19 System.out.println("false"); 20 21 } 22 23 }
當用戶按下清除緩存按鈕的函數
1 public void del(View view){ 2 3 //清除所有保存的文件 4 thisdel(); 5 6 }
5.寫完以后就可以調試啦!Lucky~
