手机内存卡转化linux,Android 往手机内存卡上存储用户名与密码的操作
當大家 用Android 應用 操作時,會發現有很多應用要登陸名和密碼,而且,它們都能記住密碼,當你退出 ,再次登陸時,你們帳號密碼會自動添加上去。
例:
布局文件 相信都能做出來 就不一一介紹 了。
下面直接來正文。
創建一個LoginActivity? 文件
public class LoginActivity extends Activity { // 聲明 獲取的用戶名與密碼的組件
public EditText edit_name, edit_pass;
// 聲明登陸按鈕對象
public Button btn_login;
// 聲明CheckBox組件對象
public CheckBox box_remember;
// 創建業務對象
public FileService fileService; @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());? // 回顯數據
Map map = fileService.readFile("private.txt");
if (map != null) {
edit_name.setText(map.get("name"));
edit_pass.setText(map.get("pass"));
} } @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();? // 判斷當前點擊組件是否是 按鈕
if (id == btn_login.getId()) {? ? // 獲取用戶名與密碼
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()) {
// 進行保存
// 調用業務對象的業務方法
LoginActivity.this.fileService.saveToRom(name, pass,
"private.txt");
Toast.makeText(LoginActivity.this, "用戶名和密碼需要保存",
Toast.LENGTH_LONG).show();? ? } else {
// 不保存
Toast.makeText(LoginActivity.this, "用戶名和密碼不需要保存",
Toast.LENGTH_LONG).show();
}? ? }? }? } }}? ? 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 readFile(String fileName){
Map map = null;
try {
FileInputStream fis = context.openFileInput(fileName);
String value = StreanTools.getValue(fis);
String values[] = value.split(":");
if(values.length >0){
map = new HashMap();
map.put("name", values[0]);
map.put("pass", values[1]);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
} }? ? 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;
}}
總結
以上是生活随笔為你收集整理的手机内存卡转化linux,Android 往手机内存卡上存储用户名与密码的操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA 基础练习题
- 下一篇: python打包工具报错_python打