当前位置:文档之家› 计科1141班胡志泉安卓实验6 - 数据存储与访问

计科1141班胡志泉安卓实验6 - 数据存储与访问

GDOU-B-11-112广东海洋大学学生实验报告书(学生用表)实验名称实验6:数据存储与访问课程名称移动编程课程号16242215x0学院(系) 数学与计算机专业计算机科学与技术班级计科1141班学生姓名学号实验地点科技楼425 实验日期2017.4.1一、实验目的1.熟悉在Android Studio开发环境下编写Android应用程序的流程;2.理解在Android Studio开发环境下进行用户界面设计的基本方法;3.掌握应用Android碎片控件Fragment开发适用于大屏幕的应用程序的方法。

4.掌握应用Android存储方法SharePreferences的应用方法。

二、实验内容在Android Studio开发环境下,使用Android的Fragment碎片控件、TextView文本标签控件、ListView列表控件、FrameLayout框架布局控件,利用SharePreferences存储方法,采用双页显示模式实现一个适用于大屏幕设备的简易新闻阅读器应用程序。

三、实验设备Android Studio四、实验结果用户界面布局设计采用了水平线性布局方式,分为左右两个碎片Fragment;其中,左侧为新闻标题列表子界面,右侧为新闻详细内容子界面。

当点击新闻标题列表中的某一标题时,右侧的Fragment将显示相应新闻标题的详细内容。

五、源代码主Activity的布局文件activity_main.xml的源代码<LinearLayout xmlns:android="/apk/res/android" xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="16dp"android:paddingBottom="16dp"android:orientation="horizontal"><FrameLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:id="@+id/fl_title"></FrameLayout><FrameLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3"android:id="@+id/fl_content"></FrameLayout></LinearLayout>新闻标题列表的布局资源文件news_title_frag.xml的源代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/news_title_list_view" /></LinearLayout>新闻标题列表子项的布局资源文件news_item.xml的源代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:singleLine="true" //singleLine属性设置为true表示该TextView只能单行显示android:ellipsize="end" //ellipsize属性用于设定当文本内容超出控件宽度时,文本的缩略方式,这里设置为end表示在尾部进行缩略android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="15dp"android:paddingBottom="15dp"android:textSize="10sp" /> //textSize属性用于设置文本大小,推荐单位为sp(放大像素, scaled pixels)</LinearLayout>新闻内容的布局资源文件news_content_frag.xml的源代码<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:padding="10dp"android:textSize="15sp" /><ImageViewandroid:layout_width="match_parent"android:layout_height="1dp"android:scaleType="fitXY" // scaleType属性设置为fitXY,表示图像将填充满整个控件的大小android:src="@drawable/spilt_line"/><TextViewandroid:id="@+id/news_content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:padding="15dp"android:textSize="10sp" /></LinearLayout><ImageViewandroid:layout_width="1dp"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:scaleType="fitXY"android:src="@drawable/spilt_line_vertical"/></RelativeLayout>Main_Activity.javapackage com.example.liheng1.mynewsapplication;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.content.Context;import android.content.SharedPreferences;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import java.util.ArrayList;public class MainActivity extends ActionBarActivity {private ArrayList<News> newsArrayList = null;private FragmentManager fManager = null;public MainActivity() {}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_main);createNewsSPData(); //调用自定函数,若程序首次运行则生成新闻的 SharedPreferences数据文件// 读取新闻SP数据文件中的数据,以此生成新闻数组列表newsArrayListnewsArrayList = new ArrayList<News>();SharedPreferences SP_NewsCount = getSharedPreferences("NewsCount", Context.MODE_PRIVATE);int newsCount = SP_NewsCount.getInt("Count",0);for (int i = 1; i <= newsCount; i++) {String FileName = "News"; // 新闻的文件名前缀SharedPreferences SP_News = getSharedPreferences(FileName + i, Context.MODE_PRIVATE);News news = new News();news.setTitle(SP_News.getString("NewsTitle",null));news.setontent(SP_News.getString("NewsContent", null));newsArrayList.add(news);}fManager = getFragmentManager();NewsTitleFragment ntFragment = new NewsTitleFragment(fManager, newsArrayList);FragmentTransaction ft = fManager.beginTransaction();//显示新闻标题:将显示新闻标题的Fragment替换掉对应位置处的FrameLayout控件ft.replace(R.id.fl_title, ntFragment);mit();}@Overrideprotected void onStop() {super.onStop();createRunRecordSPData(); //调用自定函数,生成程序首次运行记录的 SharedPreferences数据文件}private void createNewsSPData() {String RUN_RECORD = "AppRunRecord"; // 记录 App是否首次运行的SP文件的文件名String FIRST_RUN = "FirstRun"; // 首次运行的标签名String NEWS_COUNT = "NewsCount"; // 记录新闻数量的SP文件的文件名String COUNT = "Count"; // 新闻数量的标签名String FileName = "News"; // 新闻的文件名前缀SharedPreferences SP_RunRecord = getSharedPreferences(RUN_RECORD, Context.MODE_PRIVATE);if(SP_RunRecord.getBoolean(FIRST_RUN,true)){int newsCount = 20;for(int i=1;i<=newsCount;i++) {SharedPreferences SP_News = getSharedPreferences(FileName+i,Context.MODE_PRIVATE);SharedPreferences.Editor editor_News = SP_News.edit();String newsTitle = "新闻标题" + i + ":若能正常显示此标题,则说明程序已经成功一半了";String newsContent = " 这是新闻标题 " + i + " 所对应的新闻内容,内容如下:" +"Fragment很多时候都是在平板应用开发中使用的,主要是为了解决屏幕空间" +"不能充分利用的问题。

相关主题