A:使用自建立应用包下/data/data/包名下的文件保存,不需要权限
1、页面布局截图
2、页面源代码
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:padding="10dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2"
android:gravity="center"
android:text="账户:" />
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="5"
android:hint="请输入账号" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2"
android:gravity="center"
android:text="密码:" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="5"
android:hint="请输入密码"
android:inputType="textPassword" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp" >
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="是否记住密码" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="5dp" >
<Button
android:id="@+id/exit"
android:layout_width="0dp"
android:layout_weight="1"
android:height="40dp"
android:onClick="click"
android:text="退出" />
<Button
android:id="@+id/login"
android:layout_width="0dp"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:height="40dp"
android:onClick="click"
android:text="登录" />
</TableRow>
</TableLayout>
3、文件保存与读取逻辑实现代码
public class MainActivity extends Activity {
private CheckBox cb;
private EditText et_account;
private EditText et_password;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_login);
cb = (CheckBox) findViewById(R.id.cb);
et_account = (EditText) findViewById(R.id.account);
et_password = (EditText) findViewById(R.id.password);
file = new File(getFilesDir(), "config.txt");
// 读取文件信息
if (file.exists() && file.length() > 0) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
String account = line.split("#!qishuichixi!#")[0];
String password = line.split("#!qishuichixi!#")[1];
et_account.setText(account);
et_password.setText(password);
// 组件获得焦点
et_password.setFocusable(true);
et_password.setFocusableInTouchMode(true);
et_password.requestFocus();
et_password.requestFocusFromTouch();
cb.setChecked(true);
} catch (Exception e) {
Toast.makeText(this, "读取文件错误...", Toast.LENGTH_LONG).show();
}
}
}
public void click(View view) {
String account = et_account.getText().toString().trim();
String password = et_password.getText().toString().trim();
switch (view.getId()) {
case R.id.login:
if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) {
// 提示语句
Toast.makeText(this, "账号或则密码不能为空...", Toast.LENGTH_LONG).show();
// 重置空白文本
et_account.setText("");
et_password.setText("");
// 组件获得焦点
et_account.setFocusable(true);
et_account.setFocusableInTouchMode(true);
et_account.requestFocus();
et_account.requestFocusFromTouch();
return;
}
if (cb.isChecked()) {
// 第一种 保存文件信息 使用app自带文件夹
// getCacheDir() 缓存文件夹
// 不需要使用权限,即可保存下来
String text = account + "#!qishuichixi!#" + password;
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(text.getBytes("utf-8"));
outputStream.close();
Toast.makeText(this, "保存账号信息成功了...", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(this, "保存账号信息出错了...", Toast.LENGTH_LONG)
.show();
}
}
Toast.makeText(this, "登录成功...", Toast.LENGTH_LONG).show();
break;
case R.id.exit:
finish();
break;
default:
break;
}
}
}
4、文件保存路径
B:使用SD卡存储
1、新建SDActivity
public class SDActivity extends Activity {
private CheckBox cb;
private EditText et_account;
private EditText et_password;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_login);
cb = (CheckBox) findViewById(R.id.cb);
et_account = (EditText) findViewById(R.id.account);
et_password = (EditText) findViewById(R.id.password);
// sd卡没处于挂载状态
if (!Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
Toast.makeText(this, "sd卡错误...", Toast.LENGTH_LONG).show();
return;
}
// sd卡剩余空间大小 字节
long space = Environment.getExternalStorageDirectory().getFreeSpace();
// 转换大小3.98GB
String size = Formatter.formatFileSize(this, space);
System.out.println("sd卡可用空间:" + size);
// 存储路径 三种方式
String path = Environment.getExternalStorageDirectory().getPath();
// String path2="/sdcard/config.txt";
// String path3=Environment.getExternalStorageDirectory().getPath()+"/config.txt";
file = new File(path, "config.txt");
// 读取文件信息
if (file.exists() && file.length() > 0) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
String account = line.split("#!qishuichixi!#")[0];
String password = line.split("#!qishuichixi!#")[1];
et_account.setText(account);
et_password.setText(password);
// 组件获得焦点
et_password.setFocusable(true);
et_password.setFocusableInTouchMode(true);
et_password.requestFocus();
et_password.requestFocusFromTouch();
cb.setChecked(true);
} catch (Exception e) {
Toast.makeText(this, "读取文件错误...", Toast.LENGTH_LONG).show();
}
}
}
public void click(View view) {
String account = et_account.getText().toString().trim();
String password = et_password.getText().toString().trim();
switch (view.getId()) {
case R.id.login:
if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) {
// 提示语句
Toast.makeText(this, "账号或则密码不能为空...", Toast.LENGTH_LONG).show();
// 重置空白文本
et_account.setText("");
et_password.setText("");
// 组件获得焦点
et_account.setFocusable(true);
et_account.setFocusableInTouchMode(true);
et_account.requestFocus();
et_account.requestFocusFromTouch();
return;
}
if (cb.isChecked()) {
// 第一种 保存文件信息 使用app自带文件夹
// getCacheDir() 缓存文件夹
// 不需要使用权限,即可保存下来
String text = account + "#!qishuichixi!#" + password;
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(text.getBytes("utf-8"));
outputStream.close();
Toast.makeText(this, "保存账号信息成功了...", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(this, "保存账号信息出错了...", Toast.LENGTH_LONG)
.show();
}
}
Toast.makeText(this, "登录成功...", Toast.LENGTH_LONG).show();
break;
case R.id.exit:
finish();
break;
default:
break;
}
}
}
2、需要注意添加SD相关权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 允许挂载和反挂载文件系统可移动存储 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
3、设置SDActivity为启动activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
4、存储路径
C:使用shareprefer存储
1、建立SPActivity
public class SPActivity extends Activity {
private CheckBox cb;
private EditText et_account;
private EditText et_password;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_login);
cb = (CheckBox) findViewById(R.id.cb);
et_account = (EditText) findViewById(R.id.account);
et_password = (EditText) findViewById(R.id.password);
// 读取文件信息
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
String account = sp.getString("account", "");
String password = sp.getString("password", "");
et_account.setText(account);
et_password.setText(password);
if (account != "" && password != "") {
// 组件获得焦点
et_password.setFocusable(true);
et_password.setFocusableInTouchMode(true);
et_password.requestFocus();
et_password.requestFocusFromTouch();
cb.setChecked(true);
}
}
public void click(View view) {
String account = et_account.getText().toString().trim();
String password = et_password.getText().toString().trim();
switch (view.getId()) {
case R.id.login:
if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) {
// 提示语句
Toast.makeText(this, "账号或则密码不能为空...", Toast.LENGTH_LONG).show();
// 重置空白文本
et_account.setText("");
et_password.setText("");
// 组件获得焦点
et_account.setFocusable(true);
et_account.setFocusableInTouchMode(true);
et_account.requestFocus();
et_account.requestFocusFromTouch();
return;
}
if (cb.isChecked()) {
try {
// 使用SharedPreferences保存xml数据 Context.MODE_PRIVATE等于0
SharedPreferences sp = getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putString("account", account)
.putString("password", password).commit();
Toast.makeText(this, "保存账号信息成功了...", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(this, "保存账号信息出错了...", Toast.LENGTH_LONG)
.show();
}
}
Toast.makeText(this, "登录成功...", Toast.LENGTH_LONG).show();
break;
case R.id.exit:
finish();
break;
default:
break;
}
}
}
2、SharedPreferences保存xml文件截图
D:使用sqlite存储
E:源代码下载链接
http://download.csdn.net/detail/qq_30266985/9685053