为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > android_wkk_保存数据到手机内存及sd卡

android_wkk_保存数据到手机内存及sd卡

2018-04-13 11页 doc 29KB 16阅读

用户头像

is_682974

暂无简介

举报
android_wkk_保存数据到手机内存及sd卡android_wkk_保存数据到手机内存及sd卡 Android_Wkk_保存数据到手机内存及Sd 卡 //手机内存 public classAa extendsActivity implementsOnClickListener { CheckBoxcheckbox1; CheckBoxcheckbox2; Buttonbutton2; EditTextedittext1; EditTextedittext2; protected void onCreate(BundlesavedInstanceStat...
android_wkk_保存数据到手机内存及sd卡
android_wkk_保存数据到手机内存及sd卡 Android_Wkk_保存数据到手机内存及Sd 卡 //手机内存 public classAa extendsActivity implementsOnClickListener { CheckBoxcheckbox1; CheckBoxcheckbox2; Buttonbutton2; EditTextedittext1; EditTextedittext2; protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aa); checkbox1 = (CheckBox)findViewById(R.id.checkBox1); checkbox2 = (CheckBox)findViewById(R.id.checkBox2); button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(this); edittext2=(EditText)findViewById(R.id.editText2); edittext1=(EditText)findViewById(R.id.editText1); Map m=Utils.getUserInfo(); if(m!=null){ edittext1.setText(m.get("a")); edittext2.setText(m.get("b")); } } public void onClick(View v){ Stringstr1=edittext1.getText().toString(); Stringstr2=edittext2.getText().toString(); switch (v.getId()) { case R.id.checkBox1: break; case R.id.checkBox2: break; case R.id.button2: Log.d("TGA", "当此段代码运行时,会将此内容显示在TGA上"); if(!TextUtils.isEmpty(str1)&&!TextUtils.isEmpty(str2) ){ if(checkbox1.isChecked()){//判断记住密码选项是否被勾中 如果被选中则记住其帐号和密码 boolean bo=Utils.saveUserInfo(str1,str2); if(bo){//保存帐号密码并且成功时 Toast.makeText(Aa.this,"保存成功",Toast.LENGTH_LONG).show(); Intent in=new Intent(Aa.this,MainActivity.class); startActivity(in); break; }else{//帐号密码保存失败时 Toast.makeText(Aa.this,"保存失败",Toast.LENGTH_LONG).show(); break; } } }else{//帐号密码为空时 Toast.makeText(Aa.this,"别闹",Toast.LENGTH_LONG).show(); break; } Toast.makeText(Aa.this,"登录成功",Toast.LENGTH_LONG).show(); Intentin=newIntent(Aa.this,MainActivity.class); startActivity(in); break; default: break; } } } public classUtils { public static boolean saveUserInfo(String a,String b) { Stringc = "/data/data/com.example.text/utils1.txt"; Filef = newFile(c); try { FileOutputStreamfi = newFileOutputStream(f); Stringab = a + "##"+ b; fi.write(ab.getBytes()); fi.flush(); fi.close(); return true; }catch(IOException e) { e.printStackTrace(); } return false; } public staticMap getUserInfo(){ try{ Stringc = "/data/data/com.example.text/utils1.txt"; FileInputStreamifs=newFileInputStream(c); BufferedReaderbr=newBufferedReader(newInputStreamRead er(ifs)); Stringaa=br.readLine(); if(!TextUtils.isEmpty(aa)){ Stringb[]= aa.split("##"); Map ma=newHashMap(); ma.put("a", b[0]); ma.put("b", b[1]); br.close(); return ma; } br.close(); }catch(Exception e){ } return null; } } //保存数据到sd卡 //写入SD卡的权限 getUserInfo(Context context){ Stringstr= Environment.getExternalStorageState(); if(!Environment.MEDIA_MOUNTED.equals(str)){//判断sd卡是否挂载 挂载返回~true return null; } try { FilesdCardFile=Environment.getExternalStorageDirector y(); Filef=newFile(sdCardFile,"utils1.txt"); BufferedReaderbu=newBufferedReader(newInputStreamRead er(new FileInputStream(f))); Stringst=bu.readLine(); bu.close(); if(!TextUtils.isEmpty(st)){ String[]a=st.split("##"); Map m=newHashMap(); m.put("a", a[0]); m.put("b", a[1]); return m; } }catch(Exception e) { e.printStackTrace(); } return null; } } package com.example.text; import java.util.Map; import com.example.text.utils.UtilsOfSDCard; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; 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; public classAa extendsActivity implementsOnClickListener { CheckBoxcheckbox1; CheckBoxcheckbox2; Buttonbutton2; EditTextedittext1; EditTextedittext2; protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aa); // /data/data/包名/files this.getFilesDir(); checkbox1 = (CheckBox)findViewById(R.id.checkBox1); checkbox2 = (CheckBox)findViewById(R.id.checkBox2); button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(this); edittext2=(EditText)findViewById(R.id.editText2); edittext1=(EditText) findViewById(R.id.editText1); Map m=UtilsOfSDCard.getUserInfo(this); if(m!=null){ edittext1.setText(m.get("a")); edittext2.setText(m.get("b")); } } public void onClick(View v){ Stringstr1=edittext1.getText().toString(); Stringstr2=edittext2.getText().toString(); switch (v.getId()) { case R.id.checkBox1: break; case R.id.checkBox2: break; case R.id.button2: Log.d("TGA", "当此段代码运行时,会将此内容显示在TGA上"); if(!TextUtils.isEmpty(str1)&&!TextUtils.isEmpty(str2)){ if(checkbox1.isChecked()){//判断记住密码选项是否被勾中 如果被选中则记住其帐号和密码 boolean bo=UtilsOfSDCard.saveUserInfo(this,str1, str2); if(bo){//保存帐号密码并且成功时 Toast.makeText(Aa.this,"保存成功 ",Toast.LENGTH_LONG).show(); Intentin=newIntent(Aa.this,MainActivity.class); startActivity(in); break; }else{//帐号密码保存失败时 Toast.makeText(Aa.this,"保存失败",Toast.LENGTH_LONG).show(); break; } } }else{//帐号密码为空时 Toast.makeText(Aa.this,"别闹",Toast.LENGTH_LONG).show(); break; } Toast.makeText(Aa.this,"登录成功",Toast.LENGTH_LONG).show(); Intentin=newIntent(Aa.this,MainActivity.class); startActivity(in); break; default: break; } } }
/
本文档为【android_wkk_保存数据到手机内存及sd卡】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索