结对编程(开发任意AndroidAppDemo)

一、产品说明

1.编写目的:用于用户查看图书信息。

2.情景设计:本产品用于小说阅读软件。随着电子产品的普及,大部分的人群选择使用电子书,为了方便用户更清楚的了解图书的信息,明智的选择适合自己的图书而开发。

3.Demo主要实现的功能:当用户点击“图片下载”按钮时,软件会从网上下载图书的图片;当用户点击“图书简介”按钮时,页面跳转至图书介绍页面;当用户点击“返回”按钮时,返回首页。

二、实现过程

1.首先创建一个项目,然后在新建一个introduction.xml文件和Introduction.class文件,最后AndroidMainfest.xml文件中加入允许访问网络的指令<uses-permission android:name="android.permission.INTERNET" />和注册Introduction.class文件<activity android:name=".Introduction"></activity>

2.在activity_main.xml文件中加入控件

<?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"
android:gravity="center"
tools:context="com.example.pj.download.MainActivity">

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="图片下载"
android:textSize="30sp"
android:gravity="center"/>
<Button
android:id="@+id/btn_introduction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="图书简介"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>

3.在MainActivity.class文件中写入功能

package com.example.pj.download;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 MainActivity extends AppCompatActivity {

private Button btn_p;
private Button btn_i;
private ImageView img;

Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
img.setImageBitmap((Bitmap) msg.obj);
}
};

public Bitmap getInternetPicture(String UrlPath) {
HttpURLConnection httpURLConnection = null;
Bitmap bm = null;
try {
URL url = new URL(UrlPath);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(10000);
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
InputStream is = httpURLConnection.getInputStream();
bm = BitmapFactory.decodeStream(is);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return bm;
}

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

img = (ImageView) findViewById(R.id.img);
btn_p = (Button) findViewById(R.id.btn_picture);
btn_i = (Button) findViewById(R.id.btn_introduction);

btn_p.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
String URL = "https://img3.doubanio.com/lpic/s1076932.jpg";
Bitmap bm = getInternetPicture(URL);
Message msg = new Message();
msg.obj = bm;
handler.sendMessage(msg);
}
}).start();
}
});
btn_i.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Introduction.class);
startActivity(intent);
}
});


}
}

4.在introduction.xml文件中加入控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="
《三国演义》是中国第一部长篇章回体历史演义小说,以描写战争为主,反映了吴.蜀.魏三个政治集团之间的政治和军事斗争。分为黄巾之乱、董卓之乱、群雄逐鹿、三国鼎立、三国归晋五大部分。在广阔的背景上,上演了一幕幕气势磅礴的战争场面。编者罗贯中将兵法三十六计融于字里行间,既有情节,也有兵法韬略。《三国演义》反映了丰富的历史内容,人物名称、地理名称、主要事件与《三国志》基本相同。人物性格也是在《三国志》留下的固定形象基础上,才进行再发挥,进行夸张、美化、丑化等等,这也是历史演义小说的套路。《三国演义》一方面反映了真实的三国历史,照顾到读者希望了解真实历史的需要;另一方面,根据明朝社会的实际情况对三国人物进行了夸张、美化、丑化等等。
  《三国演义》是中国古典四大名著之一,全名为《三国志通俗演义》。作者是元末明初小说家罗贯中,是中国第一部长篇章回体历史演义小说。描写了从东汉末年到西晋初年之间近105年的历史风云。全书反映了三国时代的政治军事斗争,反映了三国时代各类社会斗争与矛盾的转化,并概括了这一时代的历史巨变,塑造了一批叱咤风云的三国英雄人物。"
android:textSize="16sp"/>

<Button
android:id="@+id/btn_return"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"
android:layout_marginTop="20dp"/>

</LinearLayout>

5.在Introduction.class文件中写入一个跳转功能

package com.example.pj.download;

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

/**
* Created by pj on 2017/3/28.
*/
public class Introduction extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.introduction);
Button btn_r = (Button)findViewById(R.id.btn_return);
btn_r.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Introduction.this,MainActivity.class);
startActivity(intent);
}
});
}
}

三、运行结果截图

1.下载图片


2.书籍简介


posted @ 2017-03-28 22:35  雨后无穷  阅读(394)  评论(0编辑  收藏  举报