一、設(shè)計需求
設(shè)計一個頁面有兩個編輯框,分別輸入學(xué)號和姓名。有兩個按鈕,一個是修改按鈕,當(dāng)按下修改按鈕,編輯框可以進行編輯;一個是保存按鈕,當(dāng)按下保存按鈕,使編輯框顯示當(dāng)前的內(nèi)容并且編輯框不可編輯,此外,在關(guān)閉app之后再次打開app,編輯框內(nèi)容是按下保存按鈕之后的內(nèi)容。
二、程序文件
布局文件activity_main.xml的代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:enabled="false" />
<EditText
android:id="@+id/idEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="學(xué)號"
android:enabled="false" />
<Button
android:id="@+id/modifyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存" />
</LinearLayout>
對應(yīng)的MainActivity.java文件代碼如下:
package com.example.myapplication;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText idEditText; // 學(xué)號編輯框
private EditText nameEditText; // 姓名編輯框
private Button modifyButton; // 修改按鈕
private Button saveButton; // 保存按鈕
private String currentId; // 當(dāng)前學(xué)號
private String currentName; // 當(dāng)前姓名
private boolean isEditable = false; // 編輯框是否可編輯的標(biāo)志
private SharedPreferences sharedPreferences; // SharedPreferences對象,用于存儲數(shù)據(jù)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化視圖組件
idEditText = findViewById(R.id.idEditText);
nameEditText = findViewById(R.id.nameEditText);
modifyButton = findViewById(R.id.modifyButton);
saveButton = findViewById(R.id.saveButton);
// 獲取SharedPreferences實例
sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
// 修改按鈕的點擊監(jiān)聽器
modifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isEditable = true;
idEditText.setEnabled(true);
nameEditText.setEnabled(true);
}
});
// 保存按鈕的點擊監(jiān)聽器
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isEditable = false;
currentId = idEditText.getText().toString();
currentName = nameEditText.getText().toString();
idEditText.setEnabled(false);
nameEditText.setEnabled(false);
// 保存編輯框的內(nèi)容到SharedPreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("id", currentId);
editor.putString("name", currentName);
editor.apply();
}
});
}
@Override
protected void onResume() {
super.onResume();
if (!isEditable) {
idEditText.setEnabled(false);
nameEditText.setEnabled(false);
// 從SharedPreferences加載編輯框的內(nèi)容
currentId = sharedPreferences.getString("id", "");
currentName = sharedPreferences.getString("name", "");
idEditText.setText(currentId);
nameEditText.setText(currentName);
}
}
}
代碼邏輯如下:
- onCreate() 方法:在應(yīng)用程序啟動時調(diào)用,用于初始化界面組件和設(shè)置按鈕的點擊監(jiān)聽器。
- modifyButton 點擊監(jiān)聽器:當(dāng)點擊修改按鈕時,將使編輯框變?yōu)榭删庉嫚顟B(tài)。
- saveButton 點擊監(jiān)聽器:當(dāng)點擊保存按鈕時,將獲取編輯框中的學(xué)號和姓名,并將其保存到 SharedPreferences 中。
- onResume() 方法:在應(yīng)用程序恢復(fù)活動狀態(tài)時調(diào)用,用于從 SharedPreferences 中加載保存的學(xué)號和姓名,并設(shè)置到編輯框中。
三、SharedPreferences介紹
SharedPreferences 是 Android 提供的一種輕量級的存儲機制,用于在應(yīng)用程序中存儲和檢索簡單的鍵值對數(shù)據(jù)。它是基于鍵值對的文件存儲系統(tǒng),以 XML 格式保存數(shù)據(jù),并將其存儲在應(yīng)用程序的私有目錄中。
常見的操作步驟如下:
-
獲取 SharedPreferences 實例:通過調(diào)用
getSharedPreferences()
方法或getPreferences()
方法獲取 SharedPreferences 實例。getSharedPreferences() 方法用于獲取具有自定義名稱的 SharedPreferences 實例,而 getPreferences() 方法獲取默認名稱的 SharedPreferences 實例。
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
-
編輯 SharedPreferences:通過調(diào)用 SharedPreferences 實例的
edit()
方法獲取 SharedPreferences.Editor 實例,從而可以開始編輯 SharedPreferences 文件。然后,可以使用 SharedPreferences.Editor 實例的方法來添加、修改或刪除數(shù)據(jù)。
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", "value"); // 添加或修改數(shù)據(jù)
editor.remove("key"); // 刪除數(shù)據(jù)
-
提交編輯:在完成對 SharedPreferences 文件的修改后,必須調(diào)用 SharedPreferences.Editor 實例的
apply()
方法或commit()
方法來提交編輯,以確保修改生效。
editor.apply(); // 異步提交修改
editor.commit(); // 同步提交修改
-
讀取數(shù)據(jù):通過 SharedPreferences 實例的
getXXX() 方法
(例如getString()、getInt()、getBoolean()
等)讀取已保存的數(shù)據(jù)。
String value = sharedPreferences.getString("key", defaultValue); // 讀取字符串?dāng)?shù)據(jù),可設(shè)置默認值
四、結(jié)果展示
點擊修改按鈕,可以對姓名和學(xué)號編輯框進行編輯。當(dāng)點擊保存按鈕,可以對信息進行保存,同時鎖死編輯框,使編輯框無法編輯,直到再次點擊修改按鈕。
當(dāng)APP退出重開時,會顯示上次保存的結(jié)果。
未來可期
文章到這里就要結(jié)束了,但故事還沒有結(jié)局
如果本文對你有幫助,記得點個贊??喲,也是對作者最大的鼓勵???♂?。
如有不足之處可以在評論區(qū)??多多指正,我會在看到的第一時間進行修正文章來源:http://www.zghlxwxcb.cn/news/detail-546453.html
作者:愛打瞌睡的CV君
CSDN:https://blog.csdn.net/qq_44921056
本文僅用于交流學(xué)習(xí),未經(jīng)作者允許,禁止轉(zhuǎn)載,更勿做其他用途,違者必究。文章來源地址http://www.zghlxwxcb.cn/news/detail-546453.html
到了這里,關(guān)于【Android studio】學(xué)號及姓名的輸入保存頁面的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!