我的主要活动中有两个图像,我想为每个图像单击显示新活动。当我单击第一个图像时,它会启动第二个活动(Image2.class),而不是启动第一个活动(Image1.class)。为什么会这样?我想在单击 image1 时显示 Image1.class Activity。同样,当我单击 image2 时 Image2.class Activity。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image1 = (ImageView) findViewById(R.id.imageView1);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v1) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(MainActivity.this,Image1.class);
intent1.putExtra("image1Desc", "Text Description for IMAGE1!!!!!!!!!");
startActivity(intent1);
finish();
}
});
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v2) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(MainActivity.this, Image2.class);
intent2.putExtra("image2Desc", "Text Description for IMAGE2!!!!!!!");
startActivity(intent2);
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
主要活动 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/b" />
</RelativeLayout>
Image1.class 活动 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Image1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/image1text" />
<ImageView
android:id="@+id/imageViewZoom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:src="@drawable/a" />
</RelativeLayout>
Image2.class 活动 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Image2" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/image2text" />
<ImageView
android:id="@+id/imageViewZoom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:src="@drawable/b" />
</RelativeLayout>
AndroidManifest XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageanimation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageanimation.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.imageanimation.Image1"
android:label="@string/title_activity_image1" >
</activity>
<activity
android:name="com.example.imageanimation.Image2"
android:label="@string/title_activity_image2" >
</activity>
</application>
</manifest>