Android demo

目的:领略雷锋精神
介绍:点击人物图片,会出现雷锋的图片,然后人物介绍,会有他的简介。
代码实现:
主布局:

 <ImageView
        android:id="@+id/img1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="获取人物图片"
        android:textSize="24sp"
        android:gravity="center"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="人物介绍"
        android:textSize="24sp"
        android:gravity="center"/>
</LinearLayout>

次布局:

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="
雷锋(1940年12月18日-1962年8月15日),原名雷正兴,出生于湖南望城县,中国人民解放军战士。1954年,雷锋加入中国少年先锋队;1960年,参加中国人民解放军。1962年8月15日,共产主义战士雷锋因公殉职,年仅22岁。雷锋作为中国人民解放军战士,曾立二等功一次、三等功两次。1963年3月5日,毛亲笔题词“向雷锋同志学习”,并把3月5日定为学雷锋纪念日。"
        android:textSize="20sp"/>

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

逻辑代码:
主布局代码:

private Button btn1;
       private Button btn2;
       private ImageView img1;

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            img1.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);

        img1 = (ImageView) findViewById(R.id.img1);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String URL = "http://baike.sogou.com/PicBooklet.v?relateImageGroupIds=1723668&lemmaId=46021&now=http%3A%2F%2Fpic.baike.soso.com%2Fp%2F20130701%2F20130701125417-798699310.jpg";
                        Bitmap bm = getInternetPicture(URL);
                        Message msg = new Message();
                        msg.obj = bm;
                        handler.sendMessage(msg);
                    }
                }).start();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {

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


    }

次布局逻辑代码:

 Button btn3 = (Button)findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {

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

注意:在xml类里添加入网许可<uses-permission android:name="android.permission.INTERNET" />

运行结果:图片获取失败,但是界面跳转可以

posted @ 2017-03-29 00:23  灰土豆  阅读(229)  评论(0编辑  收藏  举报