Android应用之《宋词三百首》(二)
接上回,上回我们讲到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应用之《宋词三百首》(二)的更多相关文章
- 第三百零二天 how can I 坚持
今天给掌中宝提了几个bug,确实管用,哈哈. 还有就是弟弟买房了,海亮艺术公馆,还好,至少安定下来了,可惜啊,我看好的房子也有的卖了,咋办啊. 看准的东西总是会想法设法的买了,可是无能为力啊. 还有, ...
- 第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用
第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理 使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener ...
- 第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术、设置用户代理
第三百二十八节,web爬虫讲解2—urllib库爬虫—状态吗—异常处理—浏览器伪装技术.设置用户代理 如果爬虫没有异常处理,那么爬行中一旦出现错误,程序将崩溃停止工作,有异常处理即使出现错误也能继续执 ...
- 第三百二十七节,web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求
第三百二十七节,web爬虫讲解2—urllib库爬虫 利用python系统自带的urllib库写简单爬虫 urlopen()获取一个URL的html源码read()读出html源码内容decode(& ...
- 第三百二十六节,web爬虫,scrapy模块,解决重复ur——自动递归url
第三百二十六节,web爬虫,scrapy模块,解决重复url——自动递归url 一般抓取过的url不重复抓取,那么就需要记录url,判断当前URL如果在记录里说明已经抓取过了,如果不存在说明没抓取过 ...
- 第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签
第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签 标签选择器对象 HtmlXPathSelector()创建标签选择器对象,参数接收response回调的html对象需 ...
- 第三百二十四节,web爬虫,scrapy模块介绍与使用
第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...
- 第三百二十三节,web爬虫,scrapy模块以及相关依赖模块安装
第三百二十三节,web爬虫,scrapy模块以及相关依赖模块安装 当前环境python3.5 ,windows10系统 Linux系统安装 在线安装,会自动安装scrapy模块以及相关依赖模块 pip ...
- 第三百二十二节,web爬虫,requests请求
第三百二十二节,web爬虫,requests请求 requests请求,就是用yhthon的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请 ...
随机推荐
- Oracle中常见的33个等待事件小结
在Oracle 10g中的等待事件有872个,11g中等待事件1116个. 我们可以通过v$event_name 视图来查看等待事件的相关信息 一. 等待事件的相关知识 1.1 等待事件主要可 ...
- Delphi or函数的用法
function GetFlag(a: string): Integer;var I: Integer;begin Result := 0; for I := 0 to 3 - 1 do begin ...
- android的R.java
R.java是个好东西,在Android程序开发过程中为你统一管理资源,添加ID,不可谓不犀利.不过有的时候好东西就越是娇贵,在写Android代码的时候,R.java频繁出错,搞得我是身心俱疲.数次 ...
- linux下查看硬件配置的相关命令
from:http://www.jakee.cn/index.php/archives/501.html 常用命令整理如下:查看主板的序列号: dmidecode | grep -i ’serial ...
- Delphi richedit获取选中文字
function TForm1.GetSendText(RichEdit: TExRichEdit): string;var MsgListInfo: TStrings; i, m, n: i ...
- Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- mybatis系列-05-SqlMapConfig.xml详解
mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typeHandler ...
- storm的安装配置
一.安装Zookeeper 1.设置.profile文件: export ZOOKEEPER_HOME=/home/hadoop/streamdata/zookeeper-3.4.5-cdh4.5.0 ...
- Orchard源码分析(1):插件式的支持——模块和主题
在Orchard,模块和主题都是可以插拔式的,在源码处理时,用类型(参考:DefaultExtensionTypes)区分,都没太大的本质区别,以下都称做模块. 插件的支持,实现分以下几步: 搜集模块 ...
- QTREE系列题解
打了快一星期的qtree终于打完了- - (其实还有两题改不出来弃疗了QAQ) orz神AK一星期前就虐完QTREE 避免忘记还是简单写下题解吧0 0 QTREE1 题意: 给出一颗带边权树 一个操作 ...