設計密碼鎖 , 可自行設定密碼進行比對並更新 , 以下是程式碼。
package COM.TQC.GDD02; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class GDD02 extends Activity { private String btnAdd = "Add"; private String btnLogin = "Login"; private String btnUpdate = "Update"; private String FileName = "MY_PASSWORD"; private FileInputStream fileInputStream = null; private Button Button01; private EditText EditText01; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); EditText01 = (EditText) findViewById(R.id.EditText01); Button01 = (Button) findViewById(R.id.Button01); try { fileInputStream = openFileInput(FileName+".txt"); //如果檔案不存在的話及會發生 FileNotFoundException Button01.setText(btnLogin); fileInputStream.close(); //已經確定檔案存在了, Closes this stream }catch (FileNotFoundException e) { // TODO Auto-generated catch block if(fileInputStream==null) { Button01.setText(btnAdd); } e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Button01.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(Button01.getText().equals(btnAdd)) { String text = ""+EditText01.getText(); if(!text.equals("")) { writeFile(text); showToast("新增成功!請重新登入!"); Button01.setText(btnLogin); EditText01.setText(""); } else showToast("請輸入密碼!"); } else if(Button01.getText().equals(btnLogin)) { String text = EditText01.getText().toString(); if(text.equals(readFile())) { showToast("登入成功!"); Button01.setText(btnUpdate); EditText01.setText(""); } else { showToast("登入失敗!"); EditText01.setText(""); } } else if(Button01.getText().equals(btnUpdate)) { String text = ""+EditText01.getText(); if(!text.equals("")) { writeFile(text); showToast("修改成功!請重新登入!"); Button01.setText(btnLogin); EditText01.setText(""); } else showToast("請輸入密碼!"); } }}); } private void writeFile(String data) { try { StringBuffer SBuffer = new StringBuffer(data); SBuffer.append('\n'); //使用\n字元做為字串結尾 FileOutputStream fos = openFileOutput(FileName+".txt", MODE_PRIVATE); fos.write(SBuffer.toString().getBytes()); fos.close(); }catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private String readFile() { byte[] byteArray = new byte[1024]; try { fileInputStream = this.openFileInput(FileName+".txt"); fileInputStream.read(byteArray); fileInputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String data = new String(byteArray); int index = data.indexOf("\n"); return data.substring(0, index); /* 由於byteArray長度為1024 , 將byteArray轉為字串後長度為1024 因為字串補了空白字元所以長度才會到1024 , 所以當初在writeFile的時候 才會在最後面補上\n作為結尾 , 現在取開頭到\n中間的部分做為回傳的字串 */ } private void showToast(String message) { Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG); t.show(); } }
以及簡單的 Layout檔
檔案儲存的地方在/data/data/PackageName/FileName.txt
P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....
沒有留言:
張貼留言