Android应用中用户名密码sd存储sp存储rom存储

 

 

 

public class LoginActivity extends Activity {

 // 声明 获取的用户名与密码的组件
 public EditText edit_name, edit_pass;
 // 声明登陆按钮对象
 public Button btn_login;
 // 声明CheckBox组件对象
 public CheckBox box_remember;
 // 创建业务对象
 public FileService fileService;

 // 声明 获取单选组件
 public RadioButton radio_rom, radio_sp, radio_sd;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // 设置显示视图
  setContentView(R.layout.activity_login);

  // 实例化业务对象
  fileService = new FileService(this);

  // 根据id名称获取相应组件对象
  edit_name = (EditText) findViewById(R.id.name_value);
  edit_pass = (EditText) findViewById(R.id.pass_value);
  btn_login = (Button) findViewById(R.id.but);
  box_remember = (CheckBox) findViewById(R.id.cobx);

  // 给按钮注册事件
  btn_login.setOnClickListener(new MyOnClickListener());

  radio_rom = (RadioButton) findViewById(R.id.radio_rom);
  radio_sp = (RadioButton) findViewById(R.id.radio_sp);
  radio_sd = (RadioButton) findViewById(R.id.radio_sd);

  // 实现数据返回
  SharedPreferences preferences = this.getSharedPreferences("csdn",
    Context.MODE_PRIVATE);

  edit_name.setText(preferences.getString("name", "csdn"));
  edit_pass.setText(preferences.getString("pass", "csdn"));
 }

 @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.but:

    // 获取用户名与密码
    String name = edit_name.getText().toString();
    String pass = edit_pass.getText().toString();

    // 判断用户名与密码是否为空
    if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)) {
     Toast.makeText(LoginActivity.this, "用户名或者密码不能为空",
       Toast.LENGTH_LONG).show();
     return;
    } else {
     // 判断是否记住密码
     if (box_remember.isChecked()) {

      // 判断采用什么方式保存
      if (radio_rom.isChecked()) {
       // 采用rom保存

      } else if (radio_sp.isChecked()) {
       // 采用sp保存
       boolean flag = fileService.saveBysp(name, pass,
         "csdn");
       if (flag) {
        Toast.makeText(LoginActivity.this, "保存成功",
          Toast.LENGTH_LONG).show();
       } else {
        Toast.makeText(LoginActivity.this, "保存失败",
          Toast.LENGTH_LONG).show();
       }

      } else if (radio_sd.isChecked()) {
       // 采用sd保存

      }
     }

    }

    break;

   default:
    break;
   }

  }

 }

}

 

-------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------

 

public class FileService {
 
 //上下方对象
 public Context context;
 
 public FileService(Context context){
  this.context = context;
 }
 
 /**
  * 住手机内存卡上存储 用户名与密码的操作
  * 
  * 
  */
 public boolean saveToRom(String name,String pass,String fileName){
  //上下文对象的api
  
  
  try {
   //通过 openFileOutput()方法获取一个文件 的输出流对象
   FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
  
   //拼接用户名与密码
   String result = name + ":" +pass;
   
   //写入
   fos.write(result.getBytes());
   fos.flush();
   fos.close();
  
  } catch (Exception e) {
   
   e.printStackTrace();
   return false;
  }
  return true;
 }
 
 //读取数据操作
 public Map<String, String> readFile(String fileName){
  
  Map<String ,String> map = null;
  
  try {
   FileInputStream fis = context.openFileInput(fileName);
   String value = StreanTools.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;
  
 }

 
 /**
  * 采用SharedPreferences
  * @param name
  * @param pass
  * @param fileName
  * @return
  */
 public boolean saveBysp(String name,String pass,String fileName){
  //通过上下文的api 获取 SharedPreferences对象
  SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
  //根据SharedPreferences对象的edit 方法 返回 editor对象
  Editor editor = preferences.edit();
  //存放数据
  editor.putString("name", name);
  editor.putString("pass", pass);
  
  editor.putInt("age", 8);
  editor.putBoolean("flag", true);
  
  
  //提交
  return editor.commit();

 }
 

}

-------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------

public class StreanTools {
 
 public static String getValue(FileInputStream fis)throws Exception{
  //字节 流输出流对象
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int length = -1;
  while((length = fis.read(buffer)) != -1){
   stream.write(buffer, 0, length);
  }
  stream.flush();
  stream.close();
  String value = stream.toString();
  
  
  return value;
 }

}


  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值