代码:

<?xml version="1.0"?>

-<LinearLayout android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

<EditText android:id="@+id/et_username" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="@string/input_username"/>

<EditText android:id="@+id/et_password" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="@string/input_password" android:inputType="textPassword" android:layout_marginBottom="10dp" android:layout_marginTop="10dp"/>

-<RelativeLayout android:layout_height="wrap_content" android:layout_width="fill_parent">

<CheckBox android:id="@+id/cb_rem" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/rem_password" android:layout_alignParentLeft="true" android:layout_centerVertical="true"/>

<Button android:id="@+id/bt_login" android:paddingRight="50dp" android:paddingLeft="50dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/login" android:layout_centerVertical="true" android:layout_alignParentRight="true"/>

</RelativeLayout>

</LinearLayout>

java代码

 package com.itheima.login;

 import java.io.File;
import java.util.Map; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import com.itheima.login.util.UserInfoUtil;
import com.itheima.login_sdcard.R; public class MainActivity extends Activity implements OnClickListener{ private EditText et_username;
private EditText et_password;
private CheckBox cb_rem;
private Button bt_login;
private Context mContext; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
cb_rem = (CheckBox) findViewById(R.id.cb_rem);
bt_login = (Button) findViewById(R.id.bt_login);
//b.设置按钮的点击事件
bt_login.setOnClickListener(this); //f.回显用户名密码 ??
Map<String, String> map = UserInfoUtil.getUserInfo(mContext);//获取用户名密码
if(map != null){
String username = map.get("username");
String password = map.get("password");
et_username.setText(username);//设置用户名
et_password.setText(password);
cb_rem.setChecked(true);//设置复选框选中状态
} } @SuppressLint("NewApi")
private void login(){ //c.在onclick方法中,获取用户输入的用户名密码和是否记住密码 String username = et_username.getText().toString().trim();
String password = et_password.getText().toString().trim();
boolean isrem = cb_rem.isChecked();
//d.判断用户名密码是否为空,不为空请求服务器(省略,默认请求成功)
if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
Toast.makeText(mContext, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
return ;
} //请求服务器,后面讲。。。。。。。。。。 //e.判断是否记住密码,如果记住,将用户名密码保存本地。????
if(isrem){ //判断Sdcard状态是否正常
if(!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)){
//sdcard状态是没有挂载的情况
Toast.makeText(mContext, "sdcard不存在或未挂载", Toast.LENGTH_SHORT).show();
return ;
} //判断sdcard存储空间是否满足文件的存储
File sdcard_filedir = Environment.getExternalStorageDirectory();//得到sdcard的目录作为一个文件对象
long usableSpace = sdcard_filedir.getUsableSpace();//获取文件目录对象剩余空间
long totalSpace = sdcard_filedir.getTotalSpace();
//将一个long类型的文件大小格式化成用户可以看懂的M,G字符串
String usableSpace_str = Formatter.formatFileSize(mContext, usableSpace);
String totalSpace_str = Formatter.formatFileSize(mContext, totalSpace);
if(usableSpace < 1024 * 1024 * 200){//判断剩余空间是否小于200M
Toast.makeText(mContext, "sdcard剩余空间不足,无法满足下载;剩余空间为:"+usableSpace_str, Toast.LENGTH_SHORT).show();
return ;
} boolean result = UserInfoUtil.saveUserInfo(mContext,username,password);
if(result){
Toast.makeText(mContext, "用户名密码保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext, "用户名密码保存失败", Toast.LENGTH_SHORT).show();
} }else{
Toast.makeText(mContext, "无需保存", Toast.LENGTH_SHORT).show();
} } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
login();
break; default:
break;
}
} }

里面包的代码:

 package com.itheima.login.util;

 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; public class UserInfoUtil {
//保存用户名密码
public static boolean saveUserInfo(Context context,String username, String password) { try{
String userinfo = username + "##"+ password;//封装用户名密码
// String path = "/mnt/sdcard/";//指定保存的路径
//通过Environment获取sdcard的目录
String path = Environment.getExternalStorageDirectory().getPath(); System.out.println("...............:"+path);
File file = new File(path,"userinfo.txt");//创建file
FileOutputStream fileOutputStream = new FileOutputStream(file);//创建文件写入流
fileOutputStream.write(userinfo.getBytes());//将用户名密码写入文件
fileOutputStream.close();
return true;
}catch (Exception e) {
e.printStackTrace();
} return false;
} //获取用户名密码
public static Map<String ,String> getUserInfo(Context context){ try{
//通过Environment获取sdcard的目录
String path = Environment.getExternalStorageDirectory().getPath(); System.out.println("...............:"+path);
File file = new File(path,"userinfo.txt");//创建file
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
//读取一行中包含用户密码,需要解析
String readLine = bufferedReader.readLine();
String[] split = readLine.split("##");
HashMap<String, String> hashMap = new HashMap<String ,String>();
hashMap.put("username", split[0]);
hashMap.put("password", split[1]);
bufferedReader.close();
fileInputStream.close();
return hashMap; }catch (Exception e) {
e.printStackTrace();
}
return null; } }

*在sd卡中存储   我的笔记
1.如何新赋值一个项目呢?
2.在res的string处修改app的名字
3.studio如何修改包名呢?eclipse在 android tools里面的Rename application package里面修改的
4.修改sd卡路径时,会报黄,怎么办,在清单里面修改用户权限
5.与上面一样,获得私有目录也是靠方法,但最后都要getpath()
6.可以考打印,知道是那个目录,真的是这个目录,打印看真实,还是很有用的
7.在开发文档搜,Evironment  ,会有这两个方法
8.我们还需要sd卡存不存在,如果不存在,就不需要弄了,也就是说,我们需要判断sd卡的状态
9.那我们在哪里添加sd卡状态的判断呢?
  我们用这个功能是保存数据,那么这个判断当然是在保存数据的前面判断了
  1.判断sd开的状态,用到equals方法,E.gES与那个EM里面的属性比较,相等。sd卡就登陆,就存在
  2.判断剩余空间是否满足存储
  3.调用其中一个方法时,可能要@加上新的注解
  4.1M1024*1024
  5.    //获得sd卡总空间的大小
        long total =  file.getTotalSpace();
        
        //转换数据大小的数据单位,让我们能看懂
        String totalSize = Formatter.formatFileSize(this, total);
        //获得sd卡剩余空间的大小
        long usable = file.getUsableSpace();
        
        String usableSize = Formatter.formatFileSize(this, usable);
*/
10.两种存储方法的优略
  1.私有路径,安全  缺:不能存太大的数据
  2.sd卡,只能不安全的数据,不能存密码  优点:多大都可以存

/*老师笔记
存储到SD卡(重点)

异常信息:
    09-21 23:25:32.068: W/System.err(24718): java.io.FileNotFoundException: /storage/sdcard/info.txt: open failed: EACCES (Permission denied)
   步骤:

1、    在SD卡上创建一个文件,

2、创建一个输出流往sd卡上写数据
    String data = "dsfdsae";
            
            File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
            
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data.getBytes());
            
            fos.close();

3、在清单文件中添加访问SD卡的权限
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

#7.获取SD的大小及可用空间
    //获得sd卡的目录对象
        File file = Environment.getExternalStorageDirectory();
        
        //获得sd卡总空间的大小
        long total =  file.getTotalSpace();
        
        //转换数据大小的数据单位
        String totalSize = Formatter.formatFileSize(this, total);
        //获得sd卡剩余空间的大小
        long usable = file.getUsableSpace();
        
        String usableSize = Formatter.formatFileSize(this, usable);
        
        tv.setText(usableSize+"/"+totalSize);

*/

android 登陆案例_sd卡的更多相关文章

  1. android 登陆案例_最终版本 sharedpreference

    xml  与之前的登陆案例相同 java代码: package com.itheima.login; import java.util.Map; import com.itheima.login.ut ...

  2. 广播接收者案例_sd卡状态监听

    (1)定义广播接收者 import android.content.BroadcastReceiver; import android.content.Context; import android. ...

  3. android 登陆案例

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABEMAAAJuCAIAAADU3FtnAAAgAElEQVR4nOydZ3Rc1dX3nbXez2+erC

  4. 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例

    根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习 ...

  5. 09_android入门_採用android-async-http开源项目的GET方式或POST方式实现登陆案例

    依据08_android入门_android-async-http开源项目介绍及用法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习an ...

  6. 07_android入门_採用HttpClient的POST方式、GET方式分别实现登陆案例

    1.简单介绍 HttpClient 是 Apache Jakarta Common 下的子项目,能够用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,而且它支持 HTTP 协议 ...

  7. C#基础练习(事件登陆案例)

    Form1的后台代码: namespace _08事件登陆案例 {     public partial class Form1 : Form     {         public Form1() ...

  8. Android ADT安装时卡在Calculating requirements and dependencies

    AndroidSDK及Eclipse安装都很顺利,但是在Eclipse下安装ADT插件时,先采用点击Help->installnew software->Add...,无论输入https: ...

  9. android json解析及简单例子+Android与服务器端数据交互+Android精彩案例【申明:来源于网络】

    android json解析及简单例子+Android与服务器端数据交互+Android精彩案例[申明:来源于网络] android json解析及简单例子:http://www.open-open. ...

随机推荐

  1. PTA 5-14 电话聊天狂人 (25分)

    给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数NN(\le 10^5≤10​5​​),为通话记录条数.随后NN行,每行给出一条通话记录.简单起见,这里只列出 ...

  2. ecshop中index.dwt文件分析

    对于ecshop新手来说,这篇总结值得关注. 对于没有web编程基础的同学来说,ecshop模板里面有两个文件特别重要, 但是这两个文件同时也很不好理解,分别是index.dwt和style.css. ...

  3. Python FTP多线程爆破脚本

    初学python, 自己编写了个FTP多线爆破小脚本代码很丑= = #!usr/bin/env python #!coding=utf-8 __author__='zhengjim' from ftp ...

  4. delphi 窗体透明

        TransparentColor:=true;    TransparentColorValue:=clFuchsia;    Color:= TransparentColorValue;  ...

  5. PROC简单的用例--VC连接ORACLE

    操作系统:windows 7 数据库版本号:oracle 10g VS版本号:VS2010 前言:连接ORACLE有许多方法,这里只PROC外壳,说明如何连接oracle,有事吗,希望你告诉我指出,一 ...

  6. Android仿腾讯应用宝 应用市场,下载界面, 有了进展button

    近期应用市场做,需要使用.下载与进度显示button,因此,要寻找其他大神做,直接用于改善.和很多无用的切出.在改进共享后. 再一次改变.当下载进度时,有进步.进度显示自己主动运行文本.并设置背景为灰 ...

  7. android学习日记28--Android中常用设计模式总结

    一.综述 设计模式,根据前人经验总结出常见软件工程问题的解决思想套路.GoF一共归纳了23种设计模式,当然还有人扩充,不止这些.设计模式主要利用面向对象语言的特性,而android 的设计主要用JAV ...

  8. Win7家庭普通版、家庭高级版、专业版、旗舰版版本差别

    刚才我们发了一个大图片:<Windows7.Vista.XP 三大系统功能差异比较一览图>,现在,再发一张对比图片,简要的看看Windows7家庭普通版.家庭高级版.专业版.旗舰版这四个版 ...

  9. CSS_样式sample

    <!DOCTYPE HTML> <html> <head> <title>div浮动</title> <style type=&quo ...

  10. C#_delegate - 异步调用实例 BeginInvoke EndInvoke event

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...