学习目标:
1.学习JSON数据解析
2.Fragment实现底部导航栏
学习内容:
Json简介
JavaScript Object Natation, 一种轻量级的数据交换格式, 与XML一样, 广泛被采用的客户端和服务端交互的解决方案!具有良好的可读和便于快速编写的特性。
Json解析类
[
{ "id":"1","name":"基神","age":"18" },
{ "id":"2","name":"B神","age":"18" },
{ "id":"3","name":"曹神","age":"18" }
]
2.案例演示
2.1创建一个新的项目,在activity_main.xml写入代码
[
{
"id": "1",
"name": "计算机基础教程",
"press": "清华大学出版社",
"author": "李晓云",
"price": "30.5"
},
{
"id": "2",
"name": "Java程序设计",
"press": "水利水电出版社",
"author": "张国锋",
"price": "40"
},
{
"id": "3",
"name": "安卓应用开发",
"press": "北京大学出版社",
"author": "郑晓华",
"price": "60.5"
},
{
"id": "4",
"name": "PHP应用开发教程",
"press": "南京大学出版社",
"author": "滕玉国",
"price": "27.5"
}
]
2.2在project视图中新建assest文件包,在包中创建textbook.json
[
{
"id": "1",
"name": "计算机基础教程",
"press": "清华大学出版社",
"author": "李晓云",
"price": "30.5"
},
{
"id": "2",
"name": "Java程序设计",
"press": "水利水电出版社",
"author": "张国锋",
"price": "40"
},
{
"id": "3",
"name": "安卓应用开发",
"press": "北京大学出版社",
"author": "郑晓华",
"price": "60.5"
},
{
"id": "4",
"name": "PHP应用开发教程",
"press": "南京大学出版社",
"author": "滕玉国",
"price": "27.5"
}
]
2.3在主方法中写入
package com.example.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private TextView tvContent; // 显示内容的标签
private String content; // 文件内容字符串
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源标识获得控件实例
tvContent = findViewById(R.id.tvContent);
}
/**
* 读取JSON
*
* @param view
*/
public void doReadJSON(View view) {
try {
// 读取assets目录里的文件,获取字节输入流
InputStream is = getResources().getAssets().open("textbook.json");
// 获取字节输入流长度
int length = is.available();
// 定义字节缓冲区
byte[] buffer = new byte[length];
// 读取字节输入流,将数据保存在字节缓冲区里
is.read(buffer);
// 将字节缓冲区的数据转换成字符串
content =new String(buffer, "utf-8");
// 将字符串显示在标签里
tvContent.setText(content);
// 设置标签的文本颜色
tvContent.setTextColor(Color.BLUE);
// 关闭字节输入流
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解析JSON
*
* @param view
*/
public void doParseJSON(View view) {
// 判断用户是否先读取了JSON
if (content == null) {
Toast.makeText(this, "请先读取JSON!", Toast.LENGTH_LONG).show();
} else {
try {
// 清空标签内容
tvContent.setText("");
// 创建Json数组
JSONArray jsonArray = new JSONArray(content);
// 遍历Json数组
for (int i = 0; i < jsonArray.length(); i++) {
// 获取Json对象
JSONObject jsonObject = jsonArray.getJSONObject(i);
// 对于Json对象按键取值
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String press = jsonObject.getString("press");
String author = jsonObject.getString("author");
String price = jsonObject.getString("price");
// 拼接成一个图书信息
String book = "编号:" + id + "\n书名:《" + name + "》\n出版社:" + press + "\n作者:" + author + "\n单价:" + price + "\n\n";
// 将图书信息添加到标签里
tvContent.append(book);
// 设置标签的文本颜色
tvContent.setTextColor(Color.RED);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
运行结果
3.Fragment实现底部导航栏
3.1创建一个新的项目,activity_main.xml中写入
<RelativeLayout 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"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/ly_top_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/bg_topbar">
<TextView
android:id="@+id/txt_topbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="18sp"
android:textColor="@color/text_topbar"
android:text="信息"/>
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="@color/div_white"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/ly_tab_bar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/bg_white"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_channel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_channel"
android:gravity="center"
android:padding="5dp"
android:text="@string/tab_menu_alert"
android:textColor="@drawable/tab_menu_text"
android:textSize="16sp" />
<TextView
android:id="@+id/txt_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_message"
android:gravity="center"
android:padding="5dp"
android:text="@string/tab_menu_profile"
android:textColor="@drawable/tab_menu_text"
android:textSize="16sp" />
<TextView
android:id="@+id/txt_better"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_better"
android:gravity="center"
android:padding="5dp"
android:text="@string/tab_menu_pay"
android:textColor="@drawable/tab_menu_text"
android:textSize="16sp" />
<TextView
android:id="@+id/txt_setting"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_setting"
android:gravity="center"
android:padding="5dp"
android:text="@string/tab_menu_setting"
android:textColor="@drawable/tab_menu_text"
android:textSize="16sp"/>
</LinearLayout>
<View
android:id="@+id/div_tab_bar"
android:layout_width="match_parent"
android:layout_height="2px"
android:background="@color/div_white"
android:layout_above="@id/ly_tab_bar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ly_top_bar"
android:layout_above="@id/div_tab_bar"
android:id="@+id/ly_content">
</FrameLayout>
</RelativeLayout>
3.2在项目清单文件中写入
3.3创建一个Fragment的简单布局与类,在layout中创建一个
写入
<?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="@color/bg_white">
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="呵呵"
android:textColor="@color/text_yellow"
android:textSize="20sp"/>
</LinearLayout>
3.4创建一个新的类,并写入
package net.nell.fragmentdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
public class MyFragment extends Fragment {
private String content;
public MyFragment(String content) {
this.content = content;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_content,container,false);
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
txt_content.setText(content);
return view;
}
}
3.5在主方法中写入
package net.nell.fragmentdemo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//UI Object
private TextView txt_topbar;
private TextView txt_channel;
private TextView txt_message;
private TextView txt_better;
private TextView txt_setting;
private FrameLayout ly_content;
//Fragment Object
private MyFragment fg1,fg2,fg3,fg4;
private FragmentManager fManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
fManager = getSupportFragmentManager();
bindViews();
txt_channel.performClick(); //模拟一次点击,既进去后选择第一项
}
//UI组件初始化与事件绑定
private void bindViews() {
txt_topbar = (TextView) findViewById(R.id.txt_topbar);
txt_channel = (TextView) findViewById(R.id.txt_channel);
txt_message = (TextView) findViewById(R.id.txt_message);
txt_better = (TextView) findViewById(R.id.txt_better);
txt_setting = (TextView) findViewById(R.id.txt_setting);
ly_content = (FrameLayout) findViewById(R.id.ly_content);
txt_channel.setOnClickListener(this);
txt_message.setOnClickListener(this);
txt_better.setOnClickListener(this);
txt_setting.setOnClickListener(this);
}
//重置所有文本的选中状态
private void setSelected(){
txt_channel.setSelected(false);
txt_message.setSelected(false);
txt_better.setSelected(false);
txt_setting.setSelected(false);
}
//隐藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction){
if(fg1 != null)fragmentTransaction.hide(fg1);
if(fg2 != null)fragmentTransaction.hide(fg2);
if(fg3 != null)fragmentTransaction.hide(fg3);
if(fg4 != null)fragmentTransaction.hide(fg4);
}
@Override
public void onClick(View v) {
FragmentTransaction fTransaction = fManager.beginTransaction();
hideAllFragment(fTransaction);
switch (v.getId()){
case R.id.txt_channel:
setSelected();
txt_channel.setSelected(true);
if(fg1 == null){
fg1 = new MyFragment("第一个Fragment");
fTransaction.add(R.id.ly_content,fg1);
}else{
fTransaction.show(fg1);
}
break;
case R.id.txt_message:
setSelected();
txt_message.setSelected(true);
if(fg2 == null){
fg2 = new MyFragment("第二个Fragment");
fTransaction.add(R.id.ly_content,fg2);
}else{
fTransaction.show(fg2);
}
break;
case R.id.txt_better:
setSelected();
txt_better.setSelected(true);
if(fg3 == null){
fg3 = new MyFragment("第三个Fragment");
fTransaction.add(R.id.ly_content,fg3);
}else{
fTransaction.show(fg3);
}
break;
case R.id.txt_setting:
setSelected();
txt_setting.setSelected(true);
if(fg4 == null){
fg4 = new MyFragment("第四个Fragment");
fTransaction.add(R.id.ly_content,fg4);
}else{
fTransaction.show(fg4);
}
break;
}
fTransaction.commit();
}
}
运行结果