结对编程任意Demo
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了结对编程任意Demo相关的知识,希望对你有一定的参考价值。
1.编写目的:用于用户查看图书信息。
2.情景设计:本产品用于小说阅读软件。随着电子产品的普及,大部分的人群选择使用电子书,为了方便用户更清楚的了解图书的信息,明智的选择适合自己的图书而开发。
1.首先创建一个项目,然后在新建一个introduction.xml文件和Introduction.class文件,最后androidMainfest.xml文件中加入允许访问网络的指令<uses-permission android:name="android.permission.INTERNET" />
和注册Introduction.class文件<activity android:name=".Introduction"></activity>
<?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>
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); } }); } }
<?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=" <Button android:id="@+id/btn_return" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="返回" android:layout_marginTop="20dp"/> </LinearLayout> 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); } }); } }