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>