標籤:android
接上回,上回我們講到MainActivity裡面將所有的宋詞標題和作者顯示到介面的ListView中去,我們接下來的工作是通過點擊ListView的Item跳轉到ContentActivity裡面去顯示單個宋詞的全部內容,跳轉代碼如下:
// 為ListView的Item設定點擊監聽器mListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// 將當前被點擊的item所代表的詩詞對象的引用賦給currentSongCiGlobal.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;//字型變小按鈕private ImageView fontSmallImageView;//字型變大按鈕private ImageView fontBigImageView;//代表作者TextView和主體TextView的文本文字大小預設值private float defaultTextSize;//代表當前標題TextView的文本文字大小private float currentTitleTextSize;//代表當前作者TextView和主體TextView的文本文字大小private float currentTextSize;//代表標題TextViewprivate TextView titleTextView;//代表作者TextViewprivate TextView authTextView;//代表主要內容TextViewprivate TextView descTextView;//代表返回按鈕private Button backBtn;@Overrideprotected 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賦給類變數scsc = 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() {@Overridepublic void onClick(View v) {currentTitleTextSize++;currentTextSize++;if((currentTextSize-defaultTextSize) > 5){currentTitleTextSize--;currentTextSize--;}else{setFont();}}});//為fontSmallImageView設定監聽器fontSmallImageView.setOnClickListener(new OnClickListener() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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();}//初始化結果清單適配器resultSongCiAdapterif (resultSongCiAdapter == null) {resultSongCiAdapter = new MainListViewAdapter(MainActivity.this, resultList);}else{//通知適配器,來源資料已改變resultSongCiAdapter.notifyDataSetChanged();}mListView.setAdapter(resultSongCiAdapter);}});
自訂方法的內容是:
/** * 通過傳遞過來的參數來搜尋宋詞結果 * * @param searchType按哪種條件(標題、詞人、內容)進行搜尋 */private void searchSongCi(int searchType) {//初始化resultListif (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應用之《宋詞三百首》