对SD卡的操作
来源:互联网 发布:发表心情软件 编辑:程序博客网 时间:2024/06/06 07:39
<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="wrap_content" android:layout_height="wrap_content" android:text="请输入用户名" /> <EditText android:id="@+id/et_usename" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入密码" /> <EditText android:inputType="textPassword" android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/cb_remenber_passward" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住密码" /> <Button android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="login" android:text="登陆" > </Button> </RelativeLayout></LinearLayout>
package com.example.shenchao2;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import com.example.shenchao2.service.LoginService;public class MainActivity extends Activity {private static final String TAG = "MainActivity";private EditText ed_usename;private EditText ed_password;private CheckBox cb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ed_usename = (EditText) this.findViewById(R.id.et_usename);ed_password = (EditText) this.findViewById(R.id.password);cb = (CheckBox) this.findViewById(R.id.cb_remenber_passward);//控制回显的代码Map<String,String> map = LoginService.getSaveUserInfo(this);if (map!=null) {ed_usename.setText(map.get("usename"));//设置密码和用户名ed_password.setText(map.get("password"));}}/** * 登陆成功 * @param vew */public void login(View vew) {String usename = ed_usename.getText().toString().trim();String password = ed_password.getText().toString().trim();// 将其内容拿出来// 判断是否为空if (TextUtils.isEmpty(usename) || TextUtils.isEmpty(password)) {Toast.makeText(this, "不能为空", Toast.LENGTH_LONG).show();} else {// 登陆,是否保存密码if (cb.isChecked()) {// 保存用户密码Log.i(TAG, "需要保存用户密码!");//日志文件信息boolean result = LoginService.saveUseInfo(this, usename,password);if (result) {Toast.makeText(this, "好高兴,保存成功!", Toast.LENGTH_LONG).show();}}// 登陆发送消到服务器,服务器验证是否正确if ("zhangsan".equals(usename) && "123".equals(password)) {Toast.makeText(this, "登陆成功!", Toast.LENGTH_LONG).show();} else {Toast.makeText(this, "登陆失败!", Toast.LENGTH_LONG).show();}}}}
package com.example.shenchao2.service;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.content.Context;import android.os.Environment;import android.widget.Toast;public class LoginService {public static boolean saveUseInfo(Context context, String usename,String password) {try {//判断SD卡是否存在if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {File file = new File(Environment.getExternalStorageDirectory(),"/info.txt"); // 指定保存到SDCARD卡FileOutputStream fos = new FileOutputStream(file);fos.write((usename + "##" + password).getBytes());// getBytes()是将一个字符串转化为一个字节数组。fos.close();return true;}else {Toast.makeText(context, "不可用", 0).show();return false;}} catch (Exception e) {e.printStackTrace();return false;}}/** * 获取保存的数据 * * @param context */public static Map<String, String> getSaveUserInfo(Context context) {File file = new File(Environment.getExternalStorageDirectory(),"/info.txt");try {FileInputStream fis = new FileInputStream(file);// 缓冲当前的数据BufferedReader br = new BufferedReader(new InputStreamReader(fis));String str = br.readLine(); // 读取第一行的数据String[] infos = str.split("##");// 分割开Map<String, String> map = new HashMap<String, String>(); // 初始化mapmap.put("usename", infos[0]);map.put("password", infos[1]);return map;} catch (Exception e) {e.printStackTrace();return null;}}}
布局文件:
0 0
- 对SD卡的操作
- android中对sd卡的操作
- Android 对SD卡的操作
- Android对SD的操作
- android--对SD卡进行相关文件的操作
- android 文件MD5加密 对SD卡的操作
- 对SD卡的读写操作-保存用户登录密码
- Android开发对SD卡的读写操作
- Android 对5.0+的外置SD卡删除操作
- 安卓系统中对SD卡的操作
- sd卡的操作
- SD卡的操作
- sd卡的操作
- Linux下对SD卡分区操作
- SD 卡的基本操作
- SD卡的初始化操作
- FileUtils类对SD卡上文件的一些操作(不全)
- android开发时如何对模拟器的SD卡进行操作?
- FTP的两种连接模式:Port模式和Pasv模式
- POJ 3390 Print Words in Lines(DP)
- Cocos2d-x开发:导出上架安装包(包含证书)
- hdu 3371 (Prim)
- Linux解压、打包、压缩、解包命令
- 对SD卡的操作
- android 自定义控件引用出错
- R语言学习(2)-向量矩阵和数组
- linux awk命令详解
- Android 屏幕适配
- UVa 389 - Basically Speaking
- jqueryui textbox的使用
- 压缩感知学习入门
- 【PAT (Advanced Level)】1027. Colors in Mars (20)