Android Studio基础学习(入门二)——心情语录【RelativeLayout相对布局】

在经过简单的学习后,通过学习B站大佬【极客开发者】的视频后,跟着写了这样的【心情语录】,话不多说,上代码:


MainActivity.java类:

 

package com.zzx;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mTvShow;
    private HttpURLConnection connection;
    private InputStream inputStream;
    private BufferedReader bufferedReader;
    private int GET_DATA_SUCCESS = 101;//获取数据成功的标志

    Handler mHanler = new Handler(new Handler.Callback(){
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == GET_DATA_SUCCESS) {
                String data = msg.getData().getString("data");
                Log.i("MainActivity", data);
                mTvShow.setText(data);//把获取到的数据(文字),放到mtvshow上面
            }
            return false;
        }
    });

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

        //初始化控件
        initUI();
        //初始化数据,一进入界面就显示已经出现的心情语录
        initData();
    }

    private void initUI() {
        //获取文本框
        mTvShow = findViewById(R.id.tv_show);
        //获取按钮并且绑定监听者对象
        findViewById(R.id.btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String data=getDataFromServer();
                //创建信息对象
                Message message = Message.obtain();
                Bundle bundle=new Bundle();
                bundle.putString("data",data);
                message.setData(bundle);
                message.what=GET_DATA_SUCCESS;
                //向主线程发消息
                mHanler.sendMessage(message);

            }
        }).start();
    }

    private String getDataFromServer() {
        try {
            //1.创建URL
            URL url =new URL("https://v1.hitokoto.cn/?c=f&encode=text");
            //2.打开连接
            connection=(HttpURLConnection)url.openConnection();
            //3.判断并处理结果
            if (connection.getResponseCode()==200){
                //获取输入流
                inputStream = connection.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                StringBuilder stringBuilder = new StringBuilder();
                for (String line="";(line= bufferedReader.readLine())!=null;){
                    stringBuilder.append(line);
                }
                return stringBuilder.toString();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(bufferedReader!=null)bufferedReader.close();
                if(inputStream!=null)inputStream.close();
                if(connection!=null)connection.disconnect();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return  "";
    }
}

 

activity_main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/tu"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_show"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:padding="10dp"
        android:textSize="16dp"
        android:textColor="@android:color/background_dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/btn"
        android:text="下一条语录"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

【心情语录】演示:

Android Studio基础学习(入门二)——心情语录【RelativeLayout相对布局】


说实在的,对于Android开发我只学了两天,这个代码是之前写的,刚刚想起来发出来,也是学自B站大佬【极客开发者】的视频。

日后我会不断学习Android开发,对于Android开发的学习我也会逐渐写在这上面……

 

;