Android實(shí)現(xiàn)文件存儲(chǔ)案例
本文實(shí)例為大家分享了Android實(shí)現(xiàn)文件存儲(chǔ)的具體代碼,供大家參考,具體內(nèi)容如下
1、文件存儲(chǔ)案例
public class TestActivity extends AppCompatActivity {
private EditText mFileEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
}
private void initView() {
mFileEdit = findViewById(R.id.fileEdit);
String inputText = load();
if (!TextUtils.isEmpty(inputText)) {
mFileEdit.setText(inputText);
mFileEdit.setSelection(inputText.length());
Toast.makeText(this, "Restoring succeeded", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
String inputText = mFileEdit.getText().toString();
save(inputText);
}
// 從文件中讀取數(shù)據(jù)
public void save(String inputText) {
FileOutputStream outputStream = null;
BufferedWriter writer = null;
try {
outputStream = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 將文件存儲(chǔ)到文件中
public String load() {
FileInputStream inputStream = null;
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
inputStream = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return builder.toString();
}
}
運(yùn)行結(jié)果,Pass
2、SharePreferences存儲(chǔ)案例
public class SharePfsActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "SharePfsActivity";
private Button mSharedData;
private Button mRestoreData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_pfs);
initView();
}
private void initView() {
mSharedData = findViewById(R.id.sharedBtn);
mSharedData.setOnClickListener(this);
mRestoreData = findViewById(R.id.restoreBtn);
mRestoreData.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sharedBtn:
sharedData();
break;
case R.id.restoreBtn:
restoreData();
break;
default:
break;
}
}
private void sharedData() {
SharedPreferences.Editor editor = getSharedPreferences("shareData", MODE_PRIVATE).edit();
editor.putString("name", "功勛");
editor.putString("type", "電影");
editor.apply();
}
private void restoreData() {
SharedPreferences preferences = getSharedPreferences("shareData", MODE_PRIVATE);
String name = preferences.getString("name", "");
String type = preferences.getString("type", "");
Log.d(TAG, "名稱:" + name + ",類型:" + type);
}
}
運(yùn)行結(jié)果,Pass

3、登錄頁面,實(shí)現(xiàn)記住username和pwd功能
activity_login.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名:" />
<EditText
android:id="@+id/username"
android:layout_width="240dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼:" />
<EditText
android:id="@+id/pwd"
android:layout_width="240dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remeber password" />
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄" />
</LinearLayout>
LoginActivity .class
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private Button mLogin;
private CheckBox mRemember;
private EditText mUsername;
private EditText mPwd;
private SharedPreferences mSharedPs;
private SharedPreferences.Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
}
private void initView() {
mSharedPs = PreferenceManager.getDefaultSharedPreferences(this);
mUsername = findViewById(R.id.username);
mPwd = findViewById(R.id.pwd);
mRemember = findViewById(R.id.remember);
mLogin = findViewById(R.id.login);
boolean isRemember = mSharedPs.getBoolean("remember_pwd", false);
if (isRemember) {
// 將賬號(hào)和密碼都設(shè)置到文本框中
mUsername.setText(mSharedPs.getString("username", ""));
mPwd.setText(mSharedPs.getString("pwd", ""));
mRemember.setChecked(true);
}
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsername.getText().toString();
String pwd = mPwd.getText().toString();
// 如果賬號(hào):admin,密碼:123456,就認(rèn)為登錄成功
if (username.equals("admin") && pwd.equals("123456")) {
mEditor = mSharedPs.edit();
// 檢查復(fù)選框是否被選中
if (mRemember.isChecked()) {
mEditor.putString("username", username);
mEditor.putString("pwd", pwd);
mEditor.putBoolean("remember_pwd", true);
} else {
mEditor.clear();
}
mEditor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Log.d(TAG, "用戶名或密碼輸入錯(cuò)誤,請(qǐng)重新輸入");
}
}
});
}
}
運(yùn)行結(jié)果,Pass

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android單一實(shí)例全局可調(diào)用網(wǎng)絡(luò)加載彈窗
這篇文章主要為大家詳細(xì)介紹了Android單一實(shí)例全局可調(diào)用網(wǎng)絡(luò)加載彈窗,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android中Glide獲取圖片Path、Bitmap用法詳解
這篇文章主要介紹了Android中Glide獲取圖片Path、Bitmap用法以及代碼分析,需要的朋友們參考一下吧。2017-12-12
Android開發(fā)實(shí)現(xiàn)的標(biāo)準(zhǔn)體重計(jì)算器功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的標(biāo)準(zhǔn)體重計(jì)算器功能,結(jié)合實(shí)例形式分析了Android體重計(jì)算器的界面布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Android將項(xiàng)目導(dǎo)出為L(zhǎng)ibrary并在項(xiàng)目中使用教程
這篇文章主要介紹了Android將項(xiàng)目導(dǎo)出為L(zhǎng)ibrary并在項(xiàng)目中使用教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Android Location服務(wù)之LocationManager案例詳解
這篇文章主要介紹了Android Location服務(wù)之LocationManager案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解
這篇文章主要為大家介紹了Handler消息傳遞機(jī)制類引入及執(zhí)行流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

