Login Example
1.activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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:id="@+id/user_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/user_name"
android:layout_alignParentTop="true"
android:text="@string/user_text"
android:textSize="20sp"/>
<EditText
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/user_text"
android:inputType="text"
android:hint="@string/user_alert"
android:textSize="20sp"/>
<TextView
android:id="@+id/pass_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/user_text"
android:layout_alignBottom="@+id/pwd"
android:text="@string/pass_text"
android:textSize="20sp"/>
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/user_name"
android:layout_toRightOf="@+id/pass_text"
android:inputType="textPassword"
android:hint="@string/pass_alert"
android:textSize="20sp"/>
<CheckBoxandroid:id="@+id/check_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pass_text"
android:text="@string/check_save"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/check_save"
android:text="@string/login_text"
android:onClick="onLoginCeck"
android:textSize="20sp"
/>
</RelativeLayout>
2.MainActivity.java
packagecom.example.loginfile;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importandroid.annotation.SuppressLint;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.CheckBox;
importandroid.widget.EditText;
importandroid.widget.Toast;
publicclassMainActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//從文件中取出用戶名及密碼,并寫入到畫面中。
try{
Filefile=newFile(getFilesDir(),"login.txt");
//文件存在且不為空,才取用戶名與密碼.
if(file.exists()&&file.length()>0){
charbuffer[]=newchar[1024];
FileReaderrd=newFileReader(file);
intlen=rd.read(buffer);
rd.close();
Stringdata=newString(buffer,0,len);
Stringbuff[]=data.split(";");
if(buff.length>0){
Stringaccount=buff[0];
Stringpass=buff[1];
//System.out.println("account="+account+"--pass="+pass);
//將用戶名與密碼set到輸入框中.
((EditText)findViewById(R.id.user_name)).setText(account);
((EditText)findViewById(R.id.pwd)).setText(pass);
}
}}
catch(IOExceptione){
e.printStackTrace();
}
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}
publicvoidonLoginCeck(Viewv)throwsIOException{
//此方法主要是把用戶名與密碼保存到txt文件中.
EditTextet_userno=(EditText)findViewById(R.id.user_name);
Stringuser_no=et_userno.getText().toString();
EditTextet_pass=(EditText)findViewById(R.id.pwd);
Stringpwd=et_pass.getText().toString();
//System.out.println("user_no="+user_no);
//System.out.println("pwd="+pwd);
//用戶與密碼不能為空.
if(user_no==null||"".equalsIgnoreCase(user_no)){
Toast.makeText(getApplicationContext(),"usernamecan'tempty.",Toast.LENGTH_LONG).show();
return;
}
if(pwd==null||"".equalsIgnoreCase(pwd)){
Toast.makeText(getApplicationContext(),"passworldcannotempty.",Toast.LENGTH_LONG).show();
return;
}
System.out.println("afteruser_noempty.");
//保存復選框選中,則將用戶及密碼保存到txt文件.
Filefile=newFile(getFilesDir(),"login.txt");
CheckBoxcb=(CheckBox)findViewById(R.id.check_save);
if(cb.isChecked()){
FileWriterwt=newFileWriter(file);
wt.write(user_no+";"+pwd);
wt.close();
}
}
}
3.strings.xml
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="app_name">loginFile</string>
<stringname="action_settings">Settings</string>
<stringname="user_text">用户名称:</string>
<stringname="user_alert">pleaseinputaccount</string>
<stringname="pass_text">用户密码:</string>
<stringname="pass_alert">inputpassworld</string>
<stringname="check_save">是否保存用户及密码</string>
<stringname="login_text">登錄</string>
</resources>