Skip to content

Commit

Permalink
Merge pull request #1925 from yingbabywhen/master
Browse files Browse the repository at this point in the history
#8 #276 提交实验8代码 书籍阅读
  • Loading branch information
zengsn authored Jun 11, 2019
2 parents 5c05787 + f3ecce6 commit 050bda3
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package edu.hzuapps.androidlabs.homework;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookStore extends Activity {
private EditText et_info;
private Button btn_save;
private Button btn_read;
//定义要显示的书名和图片
private String[] bookname={"红楼梦","西游记","水浒传","三国演义",
"鲁宾逊飘流记","简·爱","傲慢与偏见","钢铁是怎样炼成的"};
private int[] imageId={R.drawable.rendream,R.drawable.westtrave,R.drawable.water,R.drawable.threecountry,
R.drawable.lulutrave,R.drawable.jianai,R.drawable.and,R.drawable.howto};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index2_activity);

//获取布局文件中的控件
et_info = (EditText) findViewById(R.id.et_info);
btn_save = (Button) findViewById(R.id.btn_save);
btn_read = (Button) findViewById(R.id.btn_read);
//设置按钮点击函数
btn_save.setOnClickListener(new ButtonListener());
btn_read.setOnClickListener(new ButtonListener());

//获取activity中的listview对象
ListView listView =(ListView) findViewById(R.id.listview);
//定义一个适配器对象list_map
List<Map<String,Object>> list_map = new ArrayList<Map<String,Object>>();
for (int i=0;i<bookname.length;i++){
//创建一个键值对的Map集合pr,用来存放名字和头像
Map<String,Object> pr = new HashMap<String,Object>();
pr.put("fengmian",imageId[i]);
pr.put("name",bookname[i]);
//把这个存放好数据的Map集合-pr,放入到list(list_map)中
list_map.add(pr);
}

SimpleAdapter simplead = new SimpleAdapter(this,list_map,R.layout.index3_activity,new String[]{"name","fengmian"},new int[]{R.id.name,R.id.fegmian});
ListView lis1 =(ListView)findViewById(R.id.listview);
lis1.setAdapter(simplead);
lis1.setOnItemClickListener(new AdapterView.OnItemClickListener(){
//list点击事件
Bundle bundle = new Bundle();
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// TODO: Implement this method
switch(position){
case 0://第一个item
startActivity(new Intent(BookStore.this,
ShowDetail.class));
case 1://第二个item
Intent intent2 = new Intent();
intent2.setClass(BookStore.this, ShowDetail.class);
startActivity(intent2);
break;
case 2://第三个item
Intent intent3 = new Intent();
intent3.setClass(BookStore.this, ShowDetail.class);
startActivity(intent3);
break;
}
}
});
}

//定义Button按钮点击事件
private class ButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_save:
String saveinfo = et_info.getText().toString().trim();
FileOutputStream fileOutputStream;
try {
fileOutputStream=openFileOutput("data.txt", Context.MODE_APPEND);
fileOutputStream.write(saveinfo.getBytes());
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(BookStore.this,"谢谢您的建议!",Toast.LENGTH_SHORT).show();
break;

case R.id.btn_read:
String content="";
try{
FileInputStream fileInputStream=openFileInput("data.txt");
byte[] buffer=new byte[fileInputStream.available()];
fileInputStream.read(buffer);
content=new String(buffer);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(BookStore.this,"您提的建议我们已经采纳:"+content,Toast.LENGTH_SHORT).show();
break;


default: break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package edu.hzuapps.androidlabs.homework;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class LocalBookShelf extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index_activity);

TextView btnOpen = (TextView) findViewById(R.id.textview_01);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LocalBookShelf.this,
BookStore.class));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package edu.hzuapps.androidlabs.homework;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ShowDetail extends AppCompatActivity implements SensorEventListener{
private Button btn;
private ImageView imageview;
//创建传感器管理器
private SensorManager sensorManager;
private Sensor sensor;
//创建手机坐标
private float lastX;
private float lastY;
private float lastZ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index4_activity);
//<---点击按钮加载网络图片代码
btn = (Button) this.findViewById(R.id.button);
imageview = (ImageView) this.findViewById(R.id.imageView);
StrictMode.setThreadPolicy(new
StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(
new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
btn.setEnabled(false);
String strURL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555265755128&di=2c37ef8866b8f9e51ebfe29f6f79ab6c&imgtype=0&src=http%3A%2F%2Fphoto.16pic.com%2F00%2F50%2F15%2F16pic_5015127_b.jpg";
try {
Bitmap bitmap = getBitmap(strURL);
imageview.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
//点击按钮加载网络图片代码--->

//<---摇一摇代码
//通过调用getSystemService()方法并传入SENSOR_SERVICE参数来创建类的实例
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//通过调用getDefaultSensor()方法并使用TYPE_ACCELEROMETER常量(动作检测(抖动,倾斜等))
// 来获取设备上特定类型的传感器
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//摇一摇代码--->
}
public Bitmap getBitmap(String path) throws IOException {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

//<---摇一摇代码
//使用onsensorchanged()方法来监视来自抖动传感器的数据
public void onSensorChanged(SensorEvent event) {
//接收传感器传回来的手机坐标信息
lastX = event.values[0];
lastY = event.values[1];
lastZ = event.values[2];
//将坐标进行运算
double shack = Math.sqrt(lastX*lastX + lastY*lastY +lastZ*lastZ);
//用来查看当前手机坐标
//Toast.makeText(Soft1714080902309Activity.this,String.valueOf(lastX),Toast.LENGTH_SHORT).show();
//判断摇晃程度,防止轻微摇晃
if(shack >= 12){
//达到点击按钮的功能
btn.setEnabled(false);
String strURL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555265755128&di=2c37ef8866b8f9e51ebfe29f6f79ab6c&imgtype=0&src=http%3A%2F%2Fphoto.16pic.com%2F00%2F50%2F15%2F16pic_5015127_b.jpg";
try {
Bitmap bitmap = getBitmap(strURL);
imageview.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

//使用onresume()和onpause()回调方法来注册和注销传感器事件侦听器
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener((SensorEventListener) this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
//注销传感器侦听器
//可以在暂停时将监听器取消,减少手机耗电
super.onPause();
sensorManager.unregisterListener((SensorEventListener) this);
}
//摇一摇代码--->
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.hzuapps.androidlabs.homework;

import android.os.Bundle;
import android.app.Activity;

public class index3 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index3_activity);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BookStore">

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="49dp"
android:gravity="center"
android:text="网络书城" />

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="400dp" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:gravity="left"
android:text="没有找到喜欢的书?填下你想找的书名,推荐给我们吧!"
android:textSize="16dp" />

<EditText
android:id="@+id/et_info"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="59dp"
android:background="@drawable/white"
android:maxHeight="80dp"
android:textColor="#009688" />

<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:text="提交推荐"
android:textSize="20dp" />

<Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="100dp"
android:text="查看我提交的推荐历史"
android:textSize="20dp" />
</RelativeLayout>

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".index3">

<ImageView
android:id="@+id/fegmian"
android:layout_width="80dp"
android:layout_height="50dp"
android:paddingLeft="10dp"/>

<TextView
android:id="@+id/name"
android:textColor="#FFCC00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>

</LinearLayout>
Loading

0 comments on commit 050bda3

Please sign in to comment.