新建一個Android 工程.這裡我選擇的是2.1即API 7,進行開發的,其它都是默認下一步下一步即可.
/datasave/res/layout/activity_main.xml
復制代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_info" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:inputType="text|number"
android:hint="@string/et_type_name"
android:id="@+id/et_name"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/et_type_password"
android:id="@+id/et_password"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/bt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:text="@string/bt_login" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="@string/cb_remember_password"
android:id="@+id/cb_password"
/>
</RelativeLayout>
</LinearLayout>
復制代碼
這裡需要介紹的是android:hint屬性,作用是輸入框裡的顯示的提示信息.相當於web項目中的placeholder.
/datasave/res/values/strings.xml
復制代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">手機qq登錄save</string>
<string name="action_settings">Settings</string>
<string name="hello_info">請輸入賬號和密碼:</string>
<string name="et_type_name">請輸入賬號</string>
<string name="et_type_password">請輸入密碼</string>
<string name="bt_login">登 錄</string>
<string name="cb_remember_password">記住密碼</string>
</resources>
復制代碼
/datasave/src/com/amos/datasave/MainActivity.java
復制代碼
package com.amos.datasave;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
String tag = "MainActivity";
EditText et_name;//用戶名
EditText et_password;//密碼
Button bt_login;//登錄按鈕
CheckBox cb_password;//單選框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
et_name = (EditText) this.findViewById(R.id.et_name);
et_password = (EditText) this.findViewById(R.id.et_password);
bt_login = (Button) this.findViewById(R.id.bt_login);
cb_password = (CheckBox) this.findViewById(R.id.cb_password);
//注冊點擊事件
bt_login.setOnClickListener(this);
}
// 點擊事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
if (cb_password.isChecked()) {//如果單選框被選中了
//保存數據到rom
new savePasswordService(this).savePasswordToFile(et_name.getText().toString(), et_password.getText().toString());
Toast.makeText(this, "保存數據成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "沒有保存數據!", Toast.LENGTH_SHORT).show();
}
}
}
}
復制代碼
注:這裡主要注意的是進行判斷時使用switch語句能更使代碼結構更清晰.
/datasave/src/com/amos/datasave/savePasswordService.java
復制代碼
package com.amos.datasave;
import java.io.FileOutputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
@SuppressLint("WorldWriteableFiles")
public class savePasswordService {
private Context context;
private String tag = "savePasswordService";
public savePasswordService(Context context) {
this.context = context;
}
public void savePasswordToFile(String name, String password) {
// 這裡設置文件的權限
String content = name + ":" + password;
Log.d(tag, "設置文件的讀寫權限");
try {
FileOutputStream fileOutput = context.openFileOutput("LoginTestConfig.txt", Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
fileOutput.write(content.getBytes());
fileOutput.flush();
fileOutput.close();
} catch (Exception e) {
Log.d(tag, "設置文件的讀寫權限失敗!");
e.printStackTrace();
}
}
}
復制代碼
注:這裡是將用戶名密碼寫到rom中的關鍵所在,主要調用了Context中的openFileOutput()方法,默認的權限是private,即其他應用程序不可讀,這裡我設置了其它應用程序可讀寫.
這裡新建一個工程叫dataread,其界面設計不變,這裡讀取到LoginTestConfig.txt,然後往裡面寫數據,實驗證明是OK的,寫入成功.
復制代碼
package com.amos.dataread;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends Activity {
private String tag = "MainActivity";
EditText et_name;
EditText et_password;
Button bt_login;
CheckBox cb_remember;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "准備讀取數據");
File file = new File("/data/data/com.amos.datasave/files/LoginTestConfig.txt");
try {
FileOutputStream fos =new FileOutputStream(file);
fos.write("112233:pwd".getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
復制代碼
如果想要實現數據回顯,那麼可以參考以下代碼:
復制代碼
package com.amos.dataread;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private String tag = "MainActivity";
EditText et_name;
EditText et_password;
Button bt_login;
CheckBox cb_remember;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) this.findViewById(R.id.et_name);
et_password = (EditText) this.findViewById(R.id.et_password);
Log.d(tag, "准備讀取數據");
File file = new File("/data/data/com.amos.datasave/files/LoginTestConfig.txt");
try {
FileInputStream fis= new FileInputStream(file);
byte[] bytes = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
while((len=fis.read(bytes))>0){
bos.write(bytes, 0, len);
}
String result = new String(bos.toByteArray());
Log.d(tag, result);
Toast.makeText(this, "讀取數據成功!"+result, Toast.LENGTH_LONG);
String name = result.split(":")[0];
String password = result.split(":")[1];
Log.d(tag,name);
Log.d(tag,password);
et_name.setText(name);
et_password.setText(password);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}