Skip to content

Commit

Permalink
Merge pull request #1980 from zglx/master
Browse files Browse the repository at this point in the history
#5 #1094 231
  • Loading branch information
zengsn authored May 19, 2018
2 parents 56037a9 + cb36814 commit 3061649
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.service.autofill.FillEventHistory;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;


public class SecActivity extends Activity implements View.OnClickListener{
/**
* 顶部内容栏
Expand All @@ -23,9 +30,11 @@ public class SecActivity extends Activity implements View.OnClickListener{
private TextView tv_nab;
/**
*
* 中间内容栏,还没有编写
* 中间内容栏
*/
private FrameLayout mBodylayout;
private ArrayAdapter<String> adapter;
private ListView listView;
/*
底部内容栏
*/
Expand All @@ -36,6 +45,8 @@ public class SecActivity extends Activity implements View.OnClickListener{
private TextView tv_play;
private ImageView iv_wenzi;
private ImageView iv_bofang;
private String[] data = { "中超第一轮精彩回放", "中超第二轮精彩回放", "中超第三轮精彩回放", "中超第四轮精彩回放","中超第五轮精彩回放" };
private String[] data1 = { "中超第一轮精彩赛事", "中超第二轮精彩赛事", "中超第三轮精彩赛事", "中超第四轮精彩赛事","中超第五轮精彩赛事" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -45,6 +56,66 @@ protected void onCreate(Bundle savedInstanceState) {
setListener();
setInitStatues();
setTopStatues(0);
String msg=read();
refreshRecord(msg);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String msg = adapter.getItem(position).toString();
if (!msg.isEmpty())
write(msg);
msg = read();
Toast.makeText(SecActivity.this,msg,Toast.LENGTH_SHORT).show();
refreshRecord(msg);
Intent intent = new Intent(SecActivity.this, ThreeActivity.class);
startActivity(intent);
}
});
}
/*
数据读取,读取最后一次观看的记录 */
public String read() {
try {
FileInputStream inStream = this.openFileInput("record.txt");
byte[] buffer = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder();
while ((hasRead = inStream.read(buffer)) != -1) {
sb.append(new String(buffer, 0, hasRead));
}
inStream.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
数据写入,写入最后一次观看的记录
*/
public void write(String msg){
// 步骤1:获取输入值
if(msg == null) return;
try {
// 步骤2:创建一个FileOutputStream对象,MODE_APPEND追加模式
FileOutputStream fos = openFileOutput("record.txt",
MODE_PRIVATE);
// 步骤3:将获取过来的值放入文件
fos.write(msg.getBytes());
// 步骤4:关闭数据流
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
添加最后一次浏览的信息
*/
private void refreshRecord(String msg)
{
TextView tv_record=(TextView)findViewById(R.id.record);
String s = tv_record.getText().toString();
tv_record.setText(read());
}
private void init()//给控件赋值
{
Expand All @@ -60,17 +131,24 @@ private void init()//给控件赋值
tv_play = (TextView) findViewById(R.id.tv_play);
iv_wenzi = (ImageView) findViewById(R.id.iv_wenzi);
iv_bofang = (ImageView)findViewById(R.id.iv_bofang);

listView = (ListView)findViewById(R.id.ni);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mCharacter:
adapter = new ArrayAdapter<String>(
SecActivity.this, android.R.layout.simple_list_item_1, data1);

listView.setAdapter(adapter);
clearBottom();
setSelectStatues(0);
break;
case R.id.mPlay:
adapter = new ArrayAdapter<String>(
SecActivity.this, android.R.layout.simple_list_item_1, data);

listView.setAdapter(adapter);
clearBottom();
setSelectStatues(1);
break;
Expand Down Expand Up @@ -152,6 +230,10 @@ private void setSelectStatues(int index)
case 0:
mCharacter.setSelected(true);
tv_characetr.setTextColor(Color.parseColor("#0097F7"));
adapter = new ArrayAdapter<String>(
SecActivity.this, android.R.layout.simple_list_item_1, data1);
listView = (ListView) findViewById(R.id.ni);
listView.setAdapter(adapter);
break;
case 1:
mPlay.setSelected(true);
Expand All @@ -168,5 +250,4 @@ private void setInitStatues()
/*
设置初始状态值
*/

}
34 changes: 18 additions & 16 deletions soft1614080902231/app/src/main/res/layout/activity_sec.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,27 @@
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="50dp"
android:background="@android:color/white"
android:orientation="vertical">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<FrameLayout
android:id="@+id/mBodylayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
android:id="@+id/ni"
android:layout_width="wrap_content"
android:layout_height="355dp" />

<TextView
android:layout_width="100dp"
android:layout_height="20dp"
android:text="上一次观看:"
android:textSize="15dp" />
<TextView
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_marginLeft="150dp"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/mBottomLayout"
Expand All @@ -70,13 +75,11 @@
android:layout_alignParentBottom="true"
android:background="#F2F2F2"
android:orientation="horizontal">

<RelativeLayout
android:id="@+id/mCharacter"
android:layout_width="0dp"
android:layout_height="55dp"
android:layout_weight="1">

<TextView
android:id="@+id/tv_character"
android:layout_width="fill_parent"
Expand Down Expand Up @@ -112,7 +115,6 @@
android:text="精彩回放"
android:textColor="#666666"
android:textSize="14sp" />

<ImageView
android:id="@+id/iv_bofang"
android:layout_width="54dp"
Expand Down
Binary file added soft1614080902231/r51.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902231/r52.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902231/r53.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902231/r54.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions soft1614080902231/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
实验五
=
一、实验目的
-
掌握在Android App中存储数据

二、实验内容
-
根据选题要求使用文件存储

将应用产生的数据保存到文件存储中;<br>
说明使用的文件存储方式:内部 or 外部;<br>
将运行结果截图。<br>
三、实验步骤
-
Item添加监听,获取Item内容,将其保存为字符串msg<br>
通过 openFileOutput("record.txt",MODE_PRIVATE);创建一个FileOutStream的对象fos,第一个参数是文件名,第二个参数是文件打开方式<br>
将msg.getBytesz转换成字节型数据,fos.write()将其写入文件中<br>
读取文件时,创建Stringbuilder对象,逐一接收openFileInput对象中文件的内容,返回其内容字符串类型 <br>
四、实验截图
-
![](https://github.com/zglx/android-labs-2018/blob/master/soft1614080902231/r51.png)<br>
![](https://github.com/zglx/android-labs-2018/blob/master/soft1614080902231/r52.png)<br>
![](https://github.com/zglx/android-labs-2018/blob/master/soft1614080902231/r53.png)<br>
![](https://github.com/zglx/android-labs-2018/blob/master/soft1614080902231/r54.png)<br>
五、实验体会
-
学会了Android的文件存储,由于先前学过Java输入输出流,所以感觉并不怎么困难

0 comments on commit 3061649

Please sign in to comment.