android(9)_数据存储与访问3_scard_login事例
16lz
2021-12-04
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lession03_login_sd" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.lession03_login_sd.LoginActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
布局文件
- <LinearLayoutxmlns: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">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/view_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/login_name"/>
- <EditText
- android:id="@+id/edit_name"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:ems="10"
- android:inputType="textPersonName">
- <requestFocus/>
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/view_pass"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/login_pass"/>
- <EditText
- android:id="@+id/edit_pass"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:ems="10"
- android:inputType="textPersonName">
- <requestFocus/>
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <RadioGroup
- android:id="@+id/radioGroup1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- tools:ignore="UselessParent">
- <RadioButton
- android:id="@+id/radio_rom"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:checked="true"
- android:text="@string/login_rom"/>
- <RadioButton
- android:id="@+id/radio_sp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/login_sp"/>
- <RadioButton
- android:id="@+id/radio_sd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/login_sd"/>
- </RadioGroup>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <Button
- android:id="@+id/button_login"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/login_login"/>
- <CheckBox
- android:id="@+id/check_remember"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="100dp"
- android:text="@string/login_remember"/>
- </LinearLayout>
- </LinearLayout>
strings.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="app_name">lession03_login</string>
- <stringname="action_settings">Settings</string>
- <stringname="hello_world">Helloworld!</string>
- <stringname="login_name">用户名</string>
- <stringname="login_pass">密码</string>
- <stringname="login_login">登陆</string>
- <stringname="login_remember">记住密码</string>
- <stringname="login_rom">rom存储</string>
- <stringname="login_sp">sp存储</string>
- <stringname="login_sd">sd存储</string>
- </resources>
字节输出流对象:StreamTools.java
- packagecom.example.lession02_login.util;
- importjava.io.ByteArrayOutputStream;
- importjava.io.FileInputStream;
- publicclassStreamTools{
- publicstaticStringgetValue(FileInputStreamfis)throwsException{
- //字节的输出流对象
- ByteArrayOutputStreamstream=newByteArrayOutputStream();
- byte[]buffer=newbyte[1024];
- intlength=-1;
- while((length=fis.read(buffer))!=-1){
- stream.write(buffer,0,length);
- }
- stream.flush();
- stream.close();
- Stringvalue=stream.toString();
- returnvalue;
- }
- }
LoginService.java
- package com.example.lession03_login_sd.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.example.lession03_login_sd.util.StreamTools;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
public class LoginService {
// 上下文对象
public Context context;
// 通过上下文对象传过来
public LoginService(Context context) {
super();
this.context = context;
}
/**
* 采用sharedPreferences方法
*
* @param name
* @param pass
* @param fileName
* @return
*/
public boolean saveToSDCard(String name, String pass, String fileName) {
// 判断是否有sdcard
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// sd写入数据
// 获取SDCard目录
File sdCardDir = Environment.getExternalStorageDirectory();
// 根据目录及创建文件的名称,创建文件
File saveFile = new File(sdCardDir, fileName);
// 写数据 输出来
try {
FileOutputStream outStream = new FileOutputStream(saveFile);
String result = name+":"+pass;
outStream.write(result.getBytes());
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
// 读取数据
public Map<String, String> readFileSDcard(String fileName) {
Map<String, String> map = null;// new HashMap<String, String>();
try {
//第一步:获取SDcard目录文件
File sdcardDir = Environment.getExternalStorageDirectory();
//第二步:根据sdcard目录及文件名称 创建文件对象
File file = new File(sdcardDir,fileName);
//第三步:根据文件对象创建文件的输入流
FileInputStream fis = new FileInputStream(file);
//第四步:利用StreamTools工具 获取文件中的内容
String value = StreamTools.getValue(fis);
//根据规则拆分
String values[] = value.split(":");
//
if (values.length > 0) {
map = new HashMap<String, String>();
map.put("name", values[0]);
map.put("pass", values[1]);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
LoginActivity.java
- package com.example.lession03_login_sd;
import java.util.Map;
import com.example.lession03_login_sd.R;
import com.example.lession03_login_sd.service.LoginService;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class LoginActivity extends Activity {
// 声明获取的用户名和密码
private EditText edit_name, edit_pass;
// 声明登陆按对象
private Button btn_login;
// 声明复选框组件对象
private Checkable box_remember;
// 声明业务对象
private LoginService loginService;
// 声明保存方式按钮
private RadioButton radio_rom, radio_sp, radio_sd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示视图
setContentView(R.layout.activity_login);
// 实例化业务对像
loginService = new LoginService(this);
// 根据id名获取相应组件对象
edit_name = (EditText) findViewById(R.id.edit_name);
edit_pass = (EditText) findViewById(R.id.edit_pass);
btn_login = (Button) findViewById(R.id.button_login);
box_remember = (Checkable) findViewById(R.id.check_remember);
radio_rom = (RadioButton) findViewById(R.id.radio_rom);
radio_sp = (RadioButton) findViewById(R.id.radio_sp);
radio_sd = (RadioButton) findViewById(R.id.radio_sd);
// 给按钮注册事件
btn_login.setOnClickListener(new MyOnclickListener());
// 采用sdcard数据进行实现数据回显
// 获取map集合对象
Map<String, String> map = loginService.readFileSDcard("csdnsdcard.txt");
if (map != null) {
// 设置回显数据
edit_name.setText(map.get("name"));
edit_pass.setText(map.get("pass"));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
// 内部类 有关点击的处理对象
class MyOnclickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.button_login:
// 获取用户名密码
String name = edit_name.getText().toString();
String pass = edit_pass.getText().toString();
// 判断用户名密码是否为空
if (TextUtils.isEmpty(name)) {
Toast.makeText(LoginActivity.this, "用户名不可 为空",
Toast.LENGTH_LONG).show();
return;
} else if (TextUtils.isEmpty(pass)) {
Toast.makeText(LoginActivity.this, " 密码不可 为空",
Toast.LENGTH_LONG).show();
return;
} else {
// 判断记住密码是否被选中
if (box_remember.isChecked()) {
// 进行保存
// 调用业务对象的业务方法
// 如果rom保存方式被选中
if (radio_rom.isChecked()) {
// 如果sp保存方式被选中
} else if (radio_sp.isChecked()) {
// 如果sd保存方式被选中
} else if (radio_sd.isChecked()) {
boolean flag = loginService.saveToSDCard(name,
pass, "csdnsdcard.txt");
if (flag) {
Toast.makeText(LoginActivity.this, "保存成功",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "保存失败",
Toast.LENGTH_LONG).show();
}
}
} else {
Toast.makeText(LoginActivity.this, "不保存密码",
Toast.LENGTH_LONG).show();
}
}
break;
default:
break;
}
}
}
}
删除SD卡文件 http://blog.csdn.net/zhaoyazhi2129/article/details/40583215
更多相关文章
- 【Android】基础1
- Android获取验证码倒计时
- android service
- android操作sdcard
- Android带进度条文件上传
- android 用到的技巧集
- android 用到的技巧集
- Android获取/清除应用数据
- Android之SurfaceView、Camera