編輯:關於 Android 程式設計
做這個小範例純粹是為了練習一點小知識,因為自己才剛入門。實作方式其實很簡單:定義一個 ListView,然後設定監聽即可。ListView 對每個條目的監聽是 setOnItemClickListener,其回調方法 onItemClick 的四個參數分別是:
parent:觸發點擊動作的 AdapterView。
view:在 AdapterView 中被點擊的視圖(由 adapter 提供)。
position:該視圖在 adapter 中的位置。
id:被點擊元素的行 id。
下面看看 XML 裡的程式碼。
1. activity_main.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/Lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
2. item.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/p1">
<TextView
android:id="@+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="30sp"
android:text="@string/name">
</TextView>
</LinearLayout>
3. model.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/Iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/p1">
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈啊哈哈">
</TextView>
</LinearLayout>
接著是 MainActivity.java,它的功能是顯示 ListView;用戶點擊其中一個 item 後,我們就能取得對應資訊,再透過 Bundle 把資料送到跳轉頁面進行展示:
package com.example.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView Lv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Lv = (ListView) findViewById(R.id.Lv);
final String[] name = new String[] { "張國榮", "張學友", "譚詠麟" };
final String[] message = new String[] {
"張國榮,1956年生於香港,歌手、演員、音樂人;影視歌多棲發展的代表之一。1977年正式出道。1983年以《風繼續吹》成名。",
"張學友,歌手、演員,1961年出生於香港,1984年獲得香港首屆十八區業余歌唱大賽冠軍正式出道,1993年發行的國語唱片《吻別》年度銷量超過400萬張。",
"譚詠麟,1950年出生於香港,籍貫廣東新會,中國香港男歌手、音樂人、演員。1973年任溫拿樂隊主音歌手,1978年溫拿樂隊解散後以個人身份發展。"
};
final int[] photo = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3 };
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("photo", R.drawable.p1);
map1.put("name", name[0]);
data.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("photo", R.drawable.p2);
map2.put("name", name[1]);
data.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("photo", R.drawable.p3);
map3.put("name", name[2]);
data.add(map3);
Lv.setAdapter(new SimpleAdapter(this, data, R.layout.item,
new String[] { "photo", "name" },
new int[] { R.id.iv, R.id.tv_name }));
Lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Bundle bundle = new Bundle();
bundle.putInt("photo", photo[arg2]);
bundle.putString("message", message[arg2]);
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setClass(MainActivity.this, MoveList.class);
Log.i("message", message[arg2]);
startActivity(intent);
}
});
}
}
這是使用者點擊後跳轉到的頁面 MoveList:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MoveList extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.model);
Bundle bundle = getIntent().getExtras();
int id = bundle.getInt("photo");
String message = bundle.getString("message");
ImageView Iv = (ImageView) findViewById(R.id.Iv);
Iv.setImageResource(id);
TextView tv = (TextView) findViewById(R.id.tv_message);
tv.setText(message);
}
}
最後提醒大家,千萬別忘了在 AndroidManifest.xml 裡註冊新頁面,這一點要特別小心。
Android 動畫完全整理:幀動畫、補間動畫與屬性動畫
在 Android 介面開發中,動畫不只是視覺裝飾,更是操作回饋、狀態轉換與使用者體驗的重要一環。一個好的動畫,能讓畫面切換更自然,也能讓使用者更容易理解目前所處的狀態。
Android 開發實作懸浮視窗:WindowManager 拖曳浮動窗完整教學
突然對懸浮視窗產生興趣,查了資料之後做了一個小 Demo。效果是:點擊按鈕後關閉當前 Activity,顯示懸浮視窗;視窗可以拖動,雙擊後消失。 它的原理其實很簡單,核
Android 網絡開發與優化:從 HTTP Client 到請求封裝
寫在前面:網絡能力為何重要 對多數 Android 應用而言,網絡是最關鍵的能力之一。無論是下載資料、上傳表單、同步狀態,還是載入圖片,背後都離不開網絡請求。然而,真正要
深入理解 Android 菜單(Menu)體系
研讀《Pro Android 3》的菜單一章後,我對 Android 整個菜單體系有了更清晰的認識,於是整理成筆記與大家分享。(順帶一提,《Pro Android 3》是