Android 实现账号密码存储:XML 与 SharedPreference 示例
以下是一个可能的布局 XML 示例,包括两个输入框用于收集账号密码,以及用于写入和读取 XML 以及 SharedPreference 的按钮。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 账号输入框 -->
<EditText
android:id="@+id/editTextAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="请输入账号"
android:inputType="text"
android:maxLines="1"
android:singleLine="true" />
<!-- 密码输入框 -->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextAccount"
android:layout_margin="16dp"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
<!-- 写入 XML 按钮 -->
<Button
android:id="@+id/buttonWriteXml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextPassword"
android:layout_margin="16dp"
android:text="写入 XML"
android:onClick="onWriteXmlClicked" />
<!-- 读取 XML 按钮 -->
<Button
android:id="@+id/buttonReadXml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonWriteXml"
android:layout_margin="16dp"
android:text="读取 XML"
android:onClick="onReadXmlClicked" />
<!-- 写入 SharedPreference 按钮 -->
<Button
android:id="@+id/buttonWriteSP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonReadXml"
android:layout_margin="16dp"
android:text="保存账号密码"
android:onClick="onWriteSPClicked" />
<!-- 读取 SharedPreference 按钮 -->
<Button
android:id="@+id/buttonReadSP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonWriteSP"
android:layout_margin="16dp"
android:text="读取账号密码"
android:onClick="onReadSPClicked" />
</RelativeLayout>
注意,这个布局只是一个示例,你可以根据你的需求做出自己的布局。
以下是相关的 Java 代码,展示了如何使用 XML 和 SharedPreference 存储和读取账号密码:
public class MainActivity extends AppCompatActivity {
private EditText mEditTextAccount;
private EditText mEditTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditTextAccount = findViewById(R.id.editTextAccount);
mEditTextPassword = findViewById(R.id.editTextPassword);
}
// 写入 XML 按钮点击事件
public void onWriteXmlClicked(View view) {
String account = mEditTextAccount.getText().toString();
String password = mEditTextPassword.getText().toString();
// 使用 XmlSerializer 写入 XML
try {
FileOutputStream fos = openFileOutput('account.xml', Context.MODE_PRIVATE);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, 'utf-8');
serializer.startDocument(null, true);
serializer.startTag(null, 'account');
serializer.startTag(null, 'username');
serializer.text(account);
serializer.endTag(null, 'username');
serializer.startTag(null, 'password');
serializer.text(password);
serializer.endTag(null, 'password');
serializer.endTag(null, 'account');
serializer.endDocument();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 读取 XML 按钮点击事件
public void onReadXmlClicked(View view) {
try {
FileInputStream fis = openFileInput('account.xml');
XmlPullParser parser = Xml.newPullParser();
parser.setInput(fis, 'utf-8');
String account = null;
String password = null;
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagName = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if ('username'.equals(tagName)) {
account = parser.nextText();
} else if ('password'.equals(tagName)) {
password = parser.nextText();
}
break;
}
eventType = parser.next();
}
fis.close();
// 将读取到的信息显示在 Toast 中
if (account != null && password != null) {
Toast.makeText(this, '账号:' + account + '\n密码:' + password, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, '读取失败', Toast.LENGTH_SHORT).show();
}
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
}
}
// 写入 SharedPreference 按钮点击事件
public void onWriteSPClicked(View view) {
String account = mEditTextAccount.getText().toString();
String password = mEditTextPassword.getText().toString();
// 使用 SharedPreference 保存账号密码
SharedPreferences sp = getSharedPreferences('account', Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString('username', account);
editor.putString('password', password);
editor.apply();
}
// 读取 SharedPreference 按钮点击事件
public void onReadSPClicked(View view) {
// 从 SharedPreference 中读取账号密码
SharedPreferences sp = getSharedPreferences('account', Context.MODE_PRIVATE);
String account = sp.getString('username', null);
String password = sp.getString('password', null);
// 将读取到的信息显示在 Toast 中
if (account != null && password != null) {
Toast.makeText(this, '账号:' + account + '\n密码:' + password, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, '读取失败', Toast.LENGTH_SHORT).show();
}
}
}
在这个代码中,我们使用了 XmlSerializer 和 XmlPullParser 来读写 XML,使用了 SharedPreferences 来保存账号密码。你可以根据你的需求做出自己的实现。
原文地址: https://www.cveoy.top/t/topic/ny50 著作权归作者所有。请勿转载和采集!