package com.example.login;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.login.service.LoginService;
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_password;
private CheckBox cb;
private RadioGroup rg_mode;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.ed_username);
et_password = (EditText) findViewById(R.id.ed_password);
cb = (CheckBox)findViewById(R.id.cb);
rg_mode = (RadioGroup)findViewById(R.id.rg_mode);
}
//Butotn监听器在布局文件里面生成login这个方法
public void login(View view) {
//将编辑框输入的用户名和密码转为字符串
String username = et_username.getText().toString().trim();
String password = et_password.getText().toString().trim();
//如果用户名和密码为空show
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和密码不能为空", 0).show();
}
//如果输入用户名和密码正确,显示"登入成功",
else if ("zhangsan".equals(username) && "123".equals(password)){
Toast.makeText(this, "登入成功", 0).show();
LoginService loginService = new LoginService();
boolean result = false;
//得到RadioGroup的id,并返回一个整型数值
int id = rg_mode.getCheckedRadioButtonId();
//如果单选按钮是记住密码,cb.isChecked()为记住密码
if (cb.isChecked()) {
switch (id) {
//根据数字返回目录的访问权限,定义1为私有,2为公有,3,为可读,4为可写
case R.id.rd_private:
result = loginService.savaUserInfo(this, username, password, 1);
case R.id.rd_public:
result = loginService.savaUserInfo(this, username, password, 2);
case R.id.rd_readable:
result = loginService.savaUserInfo(this, username, password, 3);
case R.id.rd_wrtieable:
result = loginService.savaUserInfo(this, username, password, 4);
}
if (result)
Toast.makeText(this, "保存密码成功", 0).show();
else
Toast.makeText(this, "保存密码失败", 0).show();
}
}
else
Toast.makeText(this, "登入失败", 0).show();
}
}
package com.example.login.service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
public class LoginService {
public boolean savaUserInfo(Context context, String username, String password, int mode) {
try {
/*
* File file = new File("data/data/com.example.login/info.txt");
* context.getFilesDir();帮助返回一个目录为 /data/data/包名/files/
* File file new File(context.getFilesDir(), "info.txt");
* context.openFileOutput("private", Context.MODE_PRIVATE);在目录为 /data/data/包名/files/的目录下创建一个文件名,第一对数为文件名,第二参数为权限
*/
FileOutputStream fos = null;
switch (mode) {
case 1:
fos = context.openFileOutput("private", Context.MODE_PRIVATE);
case 2:
fos = context.openFileOutput("private", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_READABLE);
case 3:
fos = context.openFileOutput("private", Context.MODE_WORLD_READABLE);
case 4:
fos = context.openFileOutput("private", Context.MODE_WORLD_WRITEABLE);
}
//用户名和密码以字节形式写入到文件中,getBytes()字符串转为字节
fos.write((username + "##" + password).getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
<LinearLayout xmlns: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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入用户名" />
<EditText
android:id="@+id/ed_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入密码" />
<EditText
android:id="@+id/ed_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//密码以"*"号显示
android:inputType="textPassword" />
<RadioGroup
android:id="@+id/rg_mode"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rd_private"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="私有" />
<RadioButton
android:id="@+id/rd_readable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可读" />
<RadioButton
android:id="@+id/rd_wrtieable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可写" />
<RadioButton
android:id="@+id/rd_public"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公有" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="login"
android:text="登入" />
</RelativeLayout>
</LinearLayout>
全部代码:
package com.example.login;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.login.service.LoginService;
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_password;
private CheckBox cb;
private RadioGroup rg_mode;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.ed_username);
et_password = (EditText) findViewById(R.id.ed_password);
cb = (CheckBox)findViewById(R.id.cb);
rg_mode = (RadioGroup)findViewById(R.id.rg_mode);
}
public void login(View view) {
String username = et_username.getText().toString().trim();
String password = et_password.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名和密码不能为空", 0).show();
}
else if ("zhangsan".equals(username) && "123".equals(password)){
//如果不为空记住密码
Toast.makeText(this, "登入成功", 0).show();
LoginService loginService = new LoginService();
boolean result = false;
int id = rg_mode.getCheckedRadioButtonId();
if (cb.isChecked()) {
switch (id) {
case R.id.rd_private:
result = loginService.savaUserInfo(this, username, password, 1);
case R.id.rd_public:
result = loginService.savaUserInfo(this, username, password, 2);
case R.id.rd_readable:
result = loginService.savaUserInfo(this, username, password, 3);
case R.id.rd_wrtieable:
result = loginService.savaUserInfo(this, username, password, 4);
}
if (result)
Toast.makeText(this, "保存密码成功", 0).show();
else
Toast.makeText(this, "保存密码失败", 0).show();
}
}
else
Toast.makeText(this, "登入失败", 0).show();
}
}
package com.example.login.service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
public class LoginService {
public boolean savaUserInfo(Context context, String username, String password, int mode) {
try {
/*
* File file = new File("data/data/com.example.login/info.txt");
* context.getFilesDir();帮助返回一个目录为 /data/data/包名/files/
* File file new File(context.getFilesDir(), "info.txt");
* context.openFileOutput("private", Context.MODE_PRIVATE);在目录为 /data/data/包名/files/的目录下创建一个文件名,第一对数为文件名,第二参数为权限
*/
FileOutputStream fos = null;
switch (mode) {
case 1:
fos = context.openFileOutput("private", Context.MODE_PRIVATE);
case 2:
fos = context.openFileOutput("private", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_READABLE);
case 3:
fos = context.openFileOutput("private", Context.MODE_WORLD_READABLE);
case 4:
fos = context.openFileOutput("private", Context.MODE_WORLD_WRITEABLE);
}
fos.write((username + "##" + password).getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
<LinearLayout xmlns: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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入用户名" />
<EditText
android:id="@+id/ed_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入密码" />
<EditText
android:id="@+id/ed_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<RadioGroup
android:id="@+id/rg_mode"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rd_private"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="私有" />
<RadioButton
android:id="@+id/rd_readable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可读" />
<RadioButton
android:id="@+id/rd_wrtieable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可写" />
<RadioButton
android:id="@+id/rd_public"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公有" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="login"
android:text="登入" />
</RelativeLayout>
</LinearLayout>