RwInRom.java
package com.ststudy.rwInRow;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by aaron on 9/1/15.
*/
public class RwInRom extends Activity {
private EditText mEtName = null;
private EditText mEtPassWord = null;
private CheckBox mCbRe = null;
private Button mBtLogin = null;
private File mFile = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFile = new File("/data/data/com.ststudy.rwInRow/test");
login();
}
public void login()
{
mEtName = (EditText) findViewById(R.id.etName);
mEtPassWord = (EditText) findViewById(R.id.etPassWord);
mCbRe = (CheckBox) findViewById(R.id.cbRe);
mBtLogin = (Button) findViewById(R.id.btLogin);
mBtLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 獲取用戶名和密碼
String _name = mEtName.getText().toString();
String _password = mEtPassWord.getText().toString();
if (mCbRe.isChecked())
{
try {
FileOutputStream _fos = new FileOutputStream(mFile);
_fos.write((_name + "##" + _password).getBytes());
_fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(RwInRom.this,"登陸成功",Toast.LENGTH_SHORT).show();
// Log.v("Login","登陸成功");
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入用戶名"/>
<EditText
android:id="@+id/etPassWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請輸入密碼"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cbRe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="下次自動登陸"/>
<Button
android:id="@+id/btLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="登陸"/>
</RelativeLayout>
</LinearLayout>
結果預覽: