メイン アクティビティに 2 つの画像があり、画像をクリックするたびに新しいアクティビティを表示したいと考えています。最初の画像をクリックすると、最初のアクティビティ (Image1.class) ではなく、2 番目のアクティビティ (Image2.class) が開始されます。なぜこうなった?image1 をクリックしたときに Image1.class アクティビティを表示したい。同様に、image2 をクリックすると Image2.class アクティビティが表示されます。
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>