接上回,上回我们讲到MainActivity里面将所有的宋词标题和作者显示到界面的ListView中去,我们接下来的工作是通过点击ListView的Item跳转到ContentActivity里面去显示单个宋词的所有内容,跳转代码例如以下:

// 为ListView的Item设置点击监听器
mListView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 将当前被点击的item所代表的诗词对象的引用赋给currentSongCi
Global.currentSongCi = scList.get(position);
// 进行界面跳转
Intent intent = new Intent(MainActivity.this,
ContentActivity.class);
startActivity(intent);
}
});

在这里,我用一个静态变量将所点击的ListView Item所代表的宋词记录下来,然后跳转到ContentActivity。

我们在来首先看一下ContentActivity的布局文件activity_content.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/ll_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg4"
android:orientation="vertical" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="46.0dip"
android:background="@drawable/toolbar" > <TextView
android:id="@id/tv_app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/app_name"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#ffffffff" /> <Button
android:id="@id/btn_back"
android:layout_width="72.0dip"
android:layout_height="46.0dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_left"
android:paddingLeft="6.0dip"
android:text="@string/back"
android:textColor="#ffffffff"
android:textSize="14.0sp" /> <ImageView
android:id="@id/iv_font_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/font_small" /> <ImageView
android:id="@id/iv_font_big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/iv_font_small"
android:src="@drawable/font_big" />
</RelativeLayout> <ScrollView
android:id="@id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_margin="8.0dip"
android:layout_weight="1.0"
android:background="@drawable/item_bg2" > <LinearLayout
android:id="@id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:id="@id/tv_title"
style="@style/black_normal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5.0dip"
android:text="标题"
android:textAppearance="?android:textAppearanceMedium" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="5.0dip"
android:background="#ff666666" /> <TextView
android:id="@id/tv_author"
style="@style/black_normal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5.0dip"
android:text="作者"
android:textAppearance="?android:textAppearanceSmall" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="5.0dip"
android:background="#ff666666" /> <TextView
android:id="@id/tv_desc"
style="@style/black_normal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5.0dip"
android:lineSpacingMultiplier="1.3"
android:text="注解"
android:textAppearance="?android:textAppearanceSmall" />
</LinearLayout>
</ScrollView> </LinearLayout>

这里,我们能够发现,一首宋词的标题、作者、内容分别相应三个TextView控件。

接着我们再来看一下ContentActivity的内容:

package com.example.songcidemo.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import com.example.songcidemo.R;
import com.example.songcidemo.bean.SongCi;
import com.example.songcidemo.util.Global; public class ContentActivity extends Activity { private SongCi sc; //字体变小button
private ImageView fontSmallImageView;
//字体变大button
private ImageView fontBigImageView; //代表作者TextView和主体TextView的文本文字大小默认值
private float defaultTextSize;
//代表当前标题TextView的文本文字大小
private float currentTitleTextSize;
//代表当前作者TextView和主体TextView的文本文字大小
private float currentTextSize; //代表标题TextView
private TextView titleTextView;
//代表作者TextView
private TextView authTextView;
//代表主要内容TextView
private TextView descTextView; //代表返回button
private Button backBtn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_content); setupViews();
} /**
* 初始化界面
*/
private void setupViews() { //通过findviewbyid()方法获取三个TextView控件
titleTextView = (TextView) findViewById(R.id.tv_title);
authTextView = (TextView) findViewById(R.id.tv_author);
descTextView = (TextView) findViewById(R.id.tv_desc); //将currentSongCi赋给类变量sc
sc = Global.currentSongCi;
//通过get()方法获取标题、作者、内容的值
String title = sc.getTitle();
String auth = sc.getAuth();
String desc = sc.getDesc(); //将宋词的内容显示到三个TextView控件
titleTextView.setText(title);
authTextView.setText(auth);
//对字符串进行HTML格式化
Spanned sp = Html.fromHtml(desc);
descTextView.setText(sp);
// descWebView.loadDataWithBaseURL("fake://not/needed", desc, "text/html",
// "utf-8", ""); Log.v("ContentActivity", "" + titleTextView.getTextSize());
Log.v("ContentActivity", "" + authTextView.getTextSize());
Log.v("ContentActivity", "" + descTextView.getTextSize()); //初始化字体大小变量
defaultTextSize = 14;
currentTitleTextSize = 20;
currentTextSize = 14; fontBigImageView = (ImageView) findViewById(R.id.iv_font_big);
fontSmallImageView = (ImageView) findViewById(R.id.iv_font_small); //为fontBigImageView设置监听器
fontBigImageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
currentTitleTextSize++;
currentTextSize++;
if((currentTextSize-defaultTextSize) > 5){
currentTitleTextSize--;
currentTextSize--;
}else{
setFont();
}
}
}); //为fontSmallImageView设置监听器
fontSmallImageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
currentTitleTextSize--;
currentTextSize--;
if((defaultTextSize-currentTextSize) > 5){
currentTitleTextSize++;
currentTextSize++;
}else{
setFont();
}
}
}); backBtn = (Button) findViewById(R.id.btn_back); //为backBtn设置监听器
backBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//返回到主界面
Intent intent = new Intent(ContentActivity.this, MainActivity.class);
startActivity(intent);
}
}); } /**
* 设置界面字体大小
*/
private void setFont(){
titleTextView.setTextSize(currentTitleTextSize);
authTextView.setTextSize(currentTextSize);
descTextView.setTextSize(currentTextSize);
} }

为了将desc里面的诸如<p><br>这写HTML元素体现出来,这里须要一点小小的转换,就是这句

Spanned sp = Html.fromHtml(desc);

这个Activity中还实现了一个功能就是能够改变界面文字的大小。

界面截图例如以下:

最后我们再来实现一个主界面的搜索功能,我的思想是这种定义一个ArrayList<SongCi> resultList这种链表,由于已经有了scList里面存储的是所有的宋词,用for循环遍历scList,将满足搜索条件的结果增加到resultList其中去,搜索完毕后就将ListView显示resultList里面的内容,关键代码例如以下:

//为searchDialogBtn设置点击监听器
searchDialogBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
switch (currentSearchRadioButtonId) {
case R.id.radio0:
searchSongCi(SEARCH_TYPE_TITLE);
break;
case R.id.radio1:
searchSongCi(SEARCH_TYPE_AUTHOR);
break;
case R.id.radio2:
searchSongCi(SEARCH_TYPE_CONTENT); default:
break;
}
//将搜索对话框消失掉
if (searchDialog.isShowing()) {
searchDialog.dismiss();
} //初始化结果列表适配器resultSongCiAdapter
if (resultSongCiAdapter == null) {
resultSongCiAdapter = new MainListViewAdapter(
MainActivity.this, resultList);
}else{
//通知适配器,源数据已改变
resultSongCiAdapter.notifyDataSetChanged();
} mListView.setAdapter(resultSongCiAdapter); }
});

自己定义方法的内容是:

/**
* 通过传递过来的參数来搜索宋词结果
*
* @param searchType 按哪种条件(标题、词人、内容)进行搜索
*/
private void searchSongCi(int searchType) { //初始化resultList
if (resultList == null) {
resultList = new ArrayList<SongCi>();
}else{
resultList.clear();
} //获取搜索关键词
String searchWords = searchEditText.getText().toString().trim();
if (searchWords == null || searchWords.equals("")) {
return;
} for (SongCi sc : scList) {
switch (searchType) {
case SEARCH_TYPE_TITLE:
String title = sc.getTitle();
//假设标题中包括关键词,就将当前的宋词对象增加到结果链表中
if (title.contains(searchWords)) {
resultList.add(sc);
}
break;
case SEARCH_TYPE_AUTHOR:
String auth = sc.getAuth();
//假设作者名中包括关键词,就将当前的宋词对象增加到结果链表中
if (auth.contains(searchWords)) {
resultList.add(sc);
}
break;
case SEARCH_TYPE_CONTENT:
String desc = sc.getDesc();
//假设内容中包括关键词,就将当前宋词对象增加到结果链表中
if (desc.contains(searchWords)) {
resultList.add(sc);
}
break; default:
break;
}
} }

搜索界面截图例如以下:

最后附上整个项目的源代码:

Android应用之《宋词三百首》

Android应用之《宋词三百首》(二)的更多相关文章

  1. 第三百零二天 how can I 坚持

    今天给掌中宝提了几个bug,确实管用,哈哈. 还有就是弟弟买房了,海亮艺术公馆,还好,至少安定下来了,可惜啊,我看好的房子也有的卖了,咋办啊. 看准的东西总是会想法设法的买了,可是无能为力啊. 还有, ...

  2. 第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理 使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener ...

  3. 第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理

    第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术.设置用户代理 如果爬虫没有异常处理,那么爬行中一旦出现错误,程序将崩溃停止工作,有异常处理即使出现错误也能继续执 ...

  4. 第三百二十七节,web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求

    第三百二十七节,web爬虫讲解2—urllib库爬虫 利用python系统自带的urllib库写简单爬虫 urlopen()获取一个URL的html源码read()读出html源码内容decode(& ...

  5. 第三百二十六节,web爬虫,scrapy模块,解决重复ur——自动递归url

    第三百二十六节,web爬虫,scrapy模块,解决重复url——自动递归url 一般抓取过的url不重复抓取,那么就需要记录url,判断当前URL如果在记录里说明已经抓取过了,如果不存在说明没抓取过 ...

  6. 第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签

    第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签 标签选择器对象 HtmlXPathSelector()创建标签选择器对象,参数接收response回调的html对象需 ...

  7. 第三百二十四节,web爬虫,scrapy模块介绍与使用

    第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...

  8. 第三百二十三节,web爬虫,scrapy模块以及相关依赖模块安装

    第三百二十三节,web爬虫,scrapy模块以及相关依赖模块安装 当前环境python3.5 ,windows10系统 Linux系统安装 在线安装,会自动安装scrapy模块以及相关依赖模块 pip ...

  9. 第三百二十二节,web爬虫,requests请求

    第三百二十二节,web爬虫,requests请求 requests请求,就是用yhthon的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请 ...

随机推荐

  1. Windows下FFmpeg快速入门

    本系列文章导航 Windows下FFmpeg快速入门 ffmpeg参数解释 mencoder和ffmpeg参数详解(Java处理视频) Java 生成视频缩略图(ffmpeg) 使用ffmpeg进行视 ...

  2. Linux编译安装Darwin Streaming Server 6.0.3。。。

    目前主流的流媒体服务器有微软的windows media server.RealNetworks的Helixserver和苹果公司的Darwin Streaming Server. 微软的window ...

  3. ECSHOP seo修改建议

    ECSHOP是一个非常优秀的商城程序,以丰富的模板.稳定开源.非常快的执行速度赢得广大网店主的青眯.可是新建站30多天,目前百度只收录了首页,而google收录正常.我检查了他的网站一切正常,没有任何 ...

  4. [转] AE中如何由IFeature 如何获取所对应的FeatureClass

    转载的原文 AE中如何由IFeature 如何获取所对应的FeatureClass   先获取FeatureClass,然后遍历Map中所有的FeatureLayer,然后比较 FeatureClas ...

  5. Java Sleep() 与 Wait()的机制原理与区别

    一.概念.原理.区别    Java中的多线程是一种抢占式的机制而不是分时机制.线程主要有以下几种状态:可运行,运行,阻塞,死亡.抢占式机制指的是有多个线程处于可运行状态,但是只有一个线程在运行. 回 ...

  6. 15、自定义Content Provider

     自定义Content Provider的步骤    1. 编写一个类,该类必须继承自ContentProvider类. 实现ContentProvider类中所有的抽象方法. 定义Content ...

  7. 【LeetCode 208】Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  8. ADO.NET 中的数据并发

    当多个用户试图同时修改数据时,需要建立控制机制来防止一个用户的修改对同时操作的其他用户所作的修改产生不利的影响.处理这种情况的系统叫做“并发控制”.并发控制的类型通常,管理数据库中的并发有三种常见的方 ...

  9. [CODEVS2603]公路修建

    题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公路.修建公路的任务由各城市共同完成.    修建工程分若干轮完成.在每一轮中,每个城市选 ...

  10. [转]使用CSS3 Grid布局实现内容优先

    使用CSS3 Grid布局实现内容优先  http://www.w3cplus.com/css3/css3-grid-layout-module.html 本文由大漠根据Rachel Andrew的& ...