Android实训九 数据存储和访问

实训9 数据存储和访问

一、【实训目的】

1、 SharedPreferences存储数据;

2、 借助Java的I/O体系实现文件的存储,

3、使用Android内置的轻量级数据库SQLite存储数据;

二、【实训内容】

1、实现下图所示的界面,实现以下功能:

1)用SharedPreferences类,当点击"写入xml文件"按钮, 把"输入你想写入内容"控件中的数据写入到login.xml文件中;

2)用SharedPreferences类,当点击"读取内容"按钮, 把login.xml文件中的数据读到到一个TextView中,并显示内容;

具体步骤:

MainActivity源文件:

java 复制代码
package com.example.hahah;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private Button write, read;
	private EditText writeText, readText;
	private String fileName = "context.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        write = (Button) findViewById(R.id.btnwritexml);
		read = (Button) findViewById(R.id.btnreadxml);
		writeText = (EditText) findViewById(R.id.writecontent);
		readText = (EditText) findViewById(R.id.readcontent);
		write.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				write(writeText.getText().toString());
			}
		});
		read.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				readText.setText(read());
			}
		});

    }

    public String read() {
		StringBuilder sbBuilder = new StringBuilder("");
		byte[] buffer = new byte[64];
		int hasRead;
		try {
			FileInputStream fis = openFileInput(fileName);
			while ((hasRead = fis.read(buffer)) != -1) {
				sbBuilder.append(new String(buffer, 0, hasRead));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sbBuilder.toString();

	}

	public void write(String str) {

		try {
			FileOutputStream fos = openFileOutput(fileName, Context.MODE_APPEND);
			PrintStream ps = new PrintStream(fos);
			ps.print(str);
			ps.close();
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

Activity_main源代码:

java 复制代码
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:stretchColumns="2">
    
    <TableRow>
        <EditText
            android:id="@+id/writecontent"
            android:layout_width="0dp"					
            android:layout_height="wrap_content"
            android:layout_weight="1.0"				
            android:hint="输入你想写入的内容" />
    </TableRow>>
    
    <TableRow>		
         <Button
            android:id="@+id/btnwritexml"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"		
            android:text="写入xml文件" />
    </TableRow>
    
    <TableRow>
        <EditText
            android:id="@+id/readcontent"
            android:layout_width="0dp"					
            android:layout_height="wrap_content"/>
    </TableRow>>
    
    <TableRow>		
         <Button
            android:id="@+id/btnreadxml"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"		
            android:text="读取内容" />
    </TableRow>
    
    <TableRow>		
         <Button
            android:id="@+id/btnwritetxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"		
            android:text="写入login.txt文件" />
    </TableRow>
    
    <TableRow>		
         <Button
            android:id="@+id/btnwritesql"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"		
            android:text="写入数据库" />
    </TableRow>
</TableLayout>   

运行结果截图:

附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!

相关推荐
Bigger4 小时前
Flutter 开发实战:解决华为 HarmonyOS 任务列表不显示 App 名称的终极指南
android·flutter·华为
利剑 -~7 小时前
mysql面试题整理
android·数据库·mysql
梁同学与Android9 小时前
Android ---【经验篇】ArrayList vs CopyOnWriteArrayList 核心区别,怎么选择?
android·java·开发语言
沐怡旸10 小时前
【翻译】adb screenrecord 帮助文档
android
lienyin11 小时前
Android 简单的SFTP服务端+客户端通信传文件
android
fatiaozhang952716 小时前
中兴B860AV5.2-U_原机安卓4.4.2系统专用_晶晨S905L3SB处理器_线刷固件包
android·电视盒子·刷机固件·机顶盒刷机·中兴b860av5.2-u
儿歌八万首16 小时前
Android 自定义 View 实战:打造一个跟随滑动的丝滑指示器
android·kotlin
我有与与症16 小时前
Kuikly 实战:手把手撸一个跨平台 AI 聊天助手 (ChatDemo)
android
恋猫de小郭17 小时前
Flutter UI 设计库解耦重构进度,官方解答未来如何适配
android·前端·flutter
apihz17 小时前
全球IP归属地查询免费API详细指南
android·服务器·网络·网络协议·tcp/ip