package com.example.lesson3_4; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import a... ...
package com.example.lesson3_4; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { // <>裡面內容1.7必須 SDK>4.4 就可以不用寫 List<Post> mList = new ArrayList<Post>(); // 佈局中的組件 LinearLayout titles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); // 關鍵佈局 titles = (LinearLayout) findViewById(R.id.titles); for (int i = 0; i < mList.size(); i++) { // 使用for創建多個TextView TextView tv = new TextView(this); // 分別設置值 tv.setText(mList.get(i).getTitle()); // 佈局可以動態的添加多個組件 titles.addView(tv); final Post post = mList.get(i); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 為每一個TextView設置一個點擊事件 Intent intent = new Intent(MainActivity.this, ContentActivity.class); // 如果需要攜帶數據,可以通過intent的put方法 // Bundle // 一個對象需要傳遞,必須實現序列化 // 內部類訪問局部變數必須final intent.putExtra("post", post); startActivity(intent); } }); } } private void initData() { // 載入數據 for (int i = 0; i < 3; i++) { mList.add(new Post("標題" + (i + 1), "內容" + (i + 1))); } } }
package com.example.lesson3_4; import java.io.Serializable; public class Post implements Serializable { private static final long serialVersionUID = -2278908915637867413L; String title; String content; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Post(String title, String content) { super(); this.title = title; this.content = content; } @Override public String toString() { return "Post [title=" + title + ", content=" + content + "]"; } }
package com.example.lesson3_4; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; public class ContentActivity extends Activity { TextView title, content; ImageView iv_back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_content); // 關鍵佈局組件 title = (TextView) findViewById(R.id.title); content = (TextView) findViewById(R.id.content); iv_back = (ImageView) findViewById(R.id.iv_back); // 獲取前一個activity傳遞的數據 Intent intent = getIntent(); Post post = (Post) intent.getSerializableExtra("post"); // 為佈局組件設置值 title.setText(post.getTitle()); content.setText(post.getContent()); // 為ImageView設置點擊事件並且返回activity iv_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/titles" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lesson3_4.MainActivity" />
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:padding="5dp" android:text="標題" android:textSize="18sp" /> <ImageView android:id="@+id/iv_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:src="@drawable/arrow_left" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#CCC" /> <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lesson3_4" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.lesson3_4.ContentActivity" > </activity> </application> </manifest>