onclickListeners不适用于两个不同的ImageView

时间:2013-04-28 12:42:58

标签: android performance android-layout android-intent android-emulator

我的主要活动中有两个图像,我想为每个图像点击显示新的活动。当我单击第一个Image时,它会启动第二个活动(Image2.class),而不是启动第一个活动(Image1.class)。为什么会这样?我想在单击image1时显示Image1.class Activity。同样,当我点击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 Activity 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 Activity 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>

1 个答案:

答案 0 :(得分:0)

我发现这个问题很简单。

MainActivity XML的imageView2在ImageView1之上重叠,这是不可见的。在我重新调整大小后,问题就解决了。

在主要活动XML

<ImageView
         android:id="@+id/imageView2"
         android:layout_width="200dp"
         android:layout_height="100dp"
         android:layout_alignLeft="@+id/imageView1"
         android:layout_below="@+id/imageView1"
         android:layout_marginTop="19dp"
         android:src="@drawable/b" />