Android开发基础控件HttpURLConnection网络请求

1、创建一个URL对象,URL对象中包含url链接地址;
2、调用URL的openConnection()方法创建HttpURLConnection对象;
3、调用HttpURLConnection对象的getInputStream()方法得到一个InputStream对象;
4、网络请求得到的结果已经在得到的这个InputStream流中,解析这个inputStream即可;
5、InputStream转成InputStreamReader对象,再将该Reader对象转成BufferReader对象;
6、BufferReader对象的readLine()方法可以按行读取流中的数据,循环搞定[……]

继续阅读

Android实战开发之Handler的基本使用

1、Android多线程之间的数据通讯通过Handler来传递Message;
2、在当前线程中创建Message对象,需要发送到哪个线程就调用哪个线程当中的Handler;
3、初始化线程在Thread的构造方法中传入一个Runnable对象(抽象类),并且实现Runnable对象中的run方法;
4、调用Thread的start()方法启动线程;
5、初始化Handler在其构造方法中传入一个Callback对象(接口),并且实现该对象的handleMessage()方法;
6、消息通过handler发送后会自动回调Callback接口中的handleMessage()方法;
7、关键代[……]

继续阅读

Android基础控件之ViewPager的使用

1、简单一点理解,ViewPager就是我们经常能看到的“轮播”控件;
2、ViewPager的使用分为两个部分:视图和适配器,类似iOS中的View和DataSource;
3、比如我们新建3个xml文件,里面什么都没有,只是背景色不同:红、绿、蓝;
4、ViewPager相对应的适配器叫PagerAdaper,创建一个PagerAdaper的子类;
5、PagerAdaper是一个抽象类,实现4个方法getCount()、isViewFromObject()、instantiateItem()、destroyItem();
6、事件监听则通过addOnPageChangeListener([……]

继续阅读

Android基础开发之Fragment的简单使用(动态加载)

1、参照Android基础开发之Fragment的简单使用(静态加载)添加两个Fragment的类及布局文件;
2、在Activity的布局文件中添加两个按钮用来进行Fragment的切换;
3、在Activity的布局文件中添加一个FrameLayout布局用于Fragment的显示;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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:weightSum="2"
    tools:context="com.yusian.fragment.MainActivity">
 
    <button
        android:id="@+id/btn_frag_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"></button>
 
    <button
        android:id="@+id/btn_frag_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"></button>
 
    <framelayout
        android:id="@+id/fl_fragment"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
    </framelayout>
</linearlayout>

4、Fragment的切换涉及到Fragment的两个类:FragmentManager与FragmentTransaction;[……]

继续阅读

Android基础开发之Fragment的简单使用(静态加载)

1、创建Fragment的布局文件,如fragment_up.xml;

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000">
    <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Fragment"
        android:textColor="#FFFFFF"></textview>
 
</linearlayout>

2、创建Fragment的子类,实现onCreateView()方法;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.yusian.fragment;
 
 
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
/**
 * Created by Sian on 2017/3/18.
 */
 
public class UpFragment extends Fragment {
    @Nullable
    @Override
    // 相当于Activity的setContentView;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_up, container, false);
    }
}

3、在Activity的布局文件中添加fragment标签[……]

继续阅读