-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2153 from cruiji/master
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
soft1614080902442/app/java/soft1614080902442main4Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902442; | ||
|
||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
import android.widget.EditText; | ||
public class soft1614080902442main4Activity extends AppCompatActivity { | ||
|
||
String hms; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_soft1614080902442main4); | ||
setTitle("睡眠大师"); | ||
|
||
} | ||
|
||
private void save(String hms) { | ||
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();//获取对象,并且命名文件的名称 | ||
editor.putString("hms", hms); //保存数据 | ||
editor.commit(); | ||
editor.clear(); | ||
Toast.makeText(soft1614080902442main4Activity.this, "保存音乐成功", Toast.LENGTH_LONG).show(); | ||
} | ||
private void readPrefs() { | ||
SharedPreferences prefs = getSharedPreferences("data", MODE_PRIVATE); //获取对象,读取data文件 | ||
String hms = prefs.getString("hms", ""); //获取文件中的数据 | ||
Toast.makeText(soft1614080902442main4Activity.this, "正在播放音乐:"+hms, Toast.LENGTH_LONG).show(); | ||
} | ||
|
||
public void onClick(View view) { //按钮的点击事件 | ||
switch (view.getId()) { | ||
case R.id.xieru: | ||
EditText ethour = (EditText)findViewById(R.id.hour); | ||
hms = ethour.getText().toString(); | ||
save(hms); //调用保存方法,将输入的姓名和年龄保存 | ||
break; | ||
case R.id.duqu: | ||
readPrefs(); //调用读取方法,将保存的文件中的姓名和年龄显示出来 | ||
break; | ||
} | ||
|
||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
soft1614080902442/app/res/layout/activity_soft1614080902442main4.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".soft1614080902442main4Activity"> | ||
|
||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
|
||
|
||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center_vertical"> | ||
|
||
<RelativeLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="60dp" | ||
|
||
android:layout_alignParentTop="true" | ||
android:background="#fff"> | ||
|
||
<EditText | ||
android:id="@+id/naoling" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
|
||
|
||
android:layout_centerVertical="true" | ||
android:hint="搜索助眠音乐" /> | ||
|
||
|
||
<ImageButton | ||
android:id="@+id/查询" | ||
android:layout_width="25dp" | ||
android:layout_height="25dp" | ||
android:layout_alignParentRight="true" | ||
android:layout_centerVertical="true" | ||
android:layout_marginRight="15dp" | ||
android:background="@drawable/search" /> | ||
|
||
</RelativeLayout> | ||
|
||
|
||
<EditText | ||
android:id="@+id/hour" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
|
||
|
||
android:layout_centerVertical="true" | ||
android:hint="查找音乐编号" /> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
|
||
android:layout_alignParentBottom="true" | ||
|
||
android:layout_marginBottom="170dp" | ||
android:orientation="horizontal"> | ||
|
||
<Button | ||
android:id="@+id/xieru" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:onClick="onClick" | ||
android:text="保存该音乐" | ||
android:textSize="20sp" /> | ||
|
||
<Button | ||
android:id="@+id/duqu" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:onClick="onClick" | ||
android:text="播放该音乐" | ||
android:textSize="20sp" /> | ||
|
||
|
||
</LinearLayout> | ||
|
||
|
||
</RelativeLayout> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 第五次实验 | ||
|
||
## 一.实验目标 | ||
### 掌握在Android App中存储数据。 | ||
## 二.实验内容 | ||
### 1.将应用产生的数据存储到数据库中; | ||
### 2.使用UML类图描述表结构; | ||
### 3.将应用运行结果截图。 | ||
## 三.实验步骤 | ||
### 1.在该实验中我选择了SharedPreferences写入读取的方法,与老师所教授方法有些不同; | ||
### 2.在main4activity.java中分别写了写和读两个方法,并且在xml文件里设置了保存音乐,播放音乐两个按钮,通过监听器实现简单的读写功能; | ||
### 3.最后查看程序能否实现所需的功能 | ||
### 4.整理实验报告 | ||
## 四.实验结果 | ||
###  | ||
###  | ||
## 五.实验体会 | ||
### 在做这个实验的时候,开始是听不懂的,感觉比较难以理解,在宿舍后,通过网上查询,安卓存储的教程后,会使用了一种比较简单的读取方法来完成该实验。 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.