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;
}
}
}