Android案例:TextView、EditText和按钮实现数据保存和读取
布局文件代码:
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:layout_width='match_parent'
android:layout_height='match_parent'
android:orientation='vertical'
android:padding='16dp'>
<TextView
android:id='@+id/textView'
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:text='请输入文本信息:' />
<EditText
android:id='@+id/editText'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:hint='请输入文本信息' />
<Button
android:id='@+id/btn_save_resource'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:text='保存到资源文件' />
<Button
android:id='@+id/btn_read_resource'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:text='读取资源文件' />
<Button
android:id='@+id/btn_save_sdcard'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:text='保存到SD卡文件' />
<Button
android:id='@+id/btn_read_sdcard'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:text='读取SD卡文件' />
</LinearLayout>
MainActivity中的逻辑代码:
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button btnSaveResource, btnReadResource, btnSaveSdcard, btnReadSdcard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnSaveResource = findViewById(R.id.btn_save_resource);
btnReadResource = findViewById(R.id.btn_read_resource);
btnSaveSdcard = findViewById(R.id.btn_save_sdcard);
btnReadSdcard = findViewById(R.id.btn_read_sdcard);
btnSaveResource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveToResource();
}
});
btnReadResource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readFromResource();
}
});
btnSaveSdcard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveToSdcard();
}
});
btnReadSdcard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readFromSdcard();
}
});
}
private void saveToResource() {
String text = editText.getText().toString();
try {
FileOutputStream fos = openFileOutput('mytext.txt', MODE_PRIVATE);
fos.write(text.getBytes());
fos.close();
Toast.makeText(this, '保存成功', Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, '保存失败', Toast.LENGTH_SHORT).show();
}
}
private void readFromResource() {
try {
FileInputStream fis = openFileInput('mytext.txt');
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String text = new String(buffer);
fis.close();
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, '读取失败', Toast.LENGTH_SHORT).show();
}
}
private void saveToSdcard() {
String text = editText.getText().toString();
try {
File file = new File(Environment.getExternalStorageDirectory(), 'mytext.txt');
FileOutputStream fos = new FileOutputStream(file);
fos.write(text.getBytes());
fos.close();
Toast.makeText(this, '保存成功', Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, '保存失败', Toast.LENGTH_SHORT).show();
}
}
private void readFromSdcard() {
try {
File file = new File(Environment.getExternalStorageDirectory(), 'mytext.txt');
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String text = new String(buffer);
fis.close();
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, '读取失败', Toast.LENGTH_SHORT).show();
}
}
}
代码说明:
- 布局文件中包含一个TextView、一个EditText和四个按钮,用于输入文本信息、保存数据和读取数据。
- MainActivity中包含四个方法:saveToResource()、readFromResource()、saveToSdcard()和readFromSdcard(),分别实现将数据保存到资源文件和SD卡文件,以及从资源文件和SD卡文件读取数据。
- 代码中使用了FileOutputStream和FileInputStream类进行文件操作,使用了Toast类显示提示信息。
使用说明:
- 在EditText控件中输入文本信息。
- 点击“保存到资源文件”按钮将文本信息保存到资源文件。
- 点击“读取资源文件”按钮从资源文件读取文本信息。
- 点击“保存到SD卡文件”按钮将文本信息保存到SD卡文件。
- 点击“读取SD卡文件”按钮从SD卡文件读取文本信息。
注意:
- 该代码示例仅供参考,实际开发中需要根据具体需求进行调整。
- 在使用SD卡文件操作时,需要在AndroidManifest.xml文件中添加访问SD卡的权限。
相关知识:
- Android基础知识
- 文件操作
- 资源文件管理
- SD卡管理
- Toast类
原文地址: https://www.cveoy.top/t/topic/oruW 著作权归作者所有。请勿转载和采集!