查看受其父onclick方法影响的onclick方法

时间:2014-01-24 17:37:37

标签: android onclick

欢迎你们, 我有相对布局和两个imageViews。 当我设置相对布局的onClick方法并为两个imageViews设置onclick方法的不同方法时。 相对布局方法取消了2个imageViews方法为什么??

    package com.example.test2;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* Remove title bar from transparent activity */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        /*Define Views*/
        final ImageView keyboardIcon = (ImageView) findViewById(R.id.keyboardIcon);
        final ImageView micIcon = (ImageView) findViewById(R.id.micIcon);
        final RelativeLayout RL = (RelativeLayout) findViewById(R.id.activity_main);

        /** Icons on click events */
        micIcon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Toast.makeText(MainActivity.this, "mic icon",
                        Toast.LENGTH_SHORT).show();
            }
        });

        keyboardIcon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Toast.makeText(MainActivity.this, "keyboard icon",
                        Toast.LENGTH_SHORT).show();
            }
        });

        final Animation moveMicIconRight = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.move_right);
        final Animation moveKeyIconLeft = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.move_left);

        micIcon.startAnimation(moveMicIconRight);
        keyboardIcon.startAnimation(moveKeyIconLeft);

        /** Relative layout on click event*/
        RL.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                Toast.makeText(MainActivity.this, "Got it!", Toast.LENGTH_SHORT)
                        .show();
            }
        });

    }
}

编辑: 我做了一些改变

package com.example.test2;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* Remove title bar from transparent activity */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        /* Define Views */
        final ImageView keyboardIcon = (ImageView) findViewById(R.id.keyboardIcon);
        final ImageView micIcon = (ImageView) findViewById(R.id.micIcon);
        final RelativeLayout RL = (RelativeLayout) findViewById(R.id.activity_main);
        RL.setOnClickListener(this);
        keyboardIcon.setOnClickListener(this);
        micIcon.setOnClickListener(this);

        final Animation moveMicIconRight = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.move_right);
        final Animation moveKeyIconLeft = AnimationUtils.loadAnimation(
                getApplicationContext(), R.anim.move_left);

        micIcon.startAnimation(moveMicIconRight);
        keyboardIcon.startAnimation(moveKeyIconLeft);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.activity_main:
            finish();
            Toast.makeText(MainActivity.this, "Got it!", Toast.LENGTH_SHORT)
                    .show();
            break;
        case R.id.keyboardIcon:
            Toast.makeText(MainActivity.this, "keyboard icon",
                    Toast.LENGTH_SHORT).show();
            break;
        case R.id.micIcon:
            Toast.makeText(MainActivity.this, "mic icon",
                    Toast.LENGTH_SHORT).show();
            break;
        }
    }
}

和我的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" 
    android:layout_gravity="center_vertical|center_horizontal"
    android:gravity="center_vertical"
    android:id="@+id/activity_main"

    >

    <ImageView
        android:id="@+id/keyboardIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/keyboard_icon" />

    <ImageView
        android:id="@+id/micIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/mic_icon" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

我认为这与你的相对布局的descendantFocusability有关。它似乎是在它的后代之前行动,甚至阻止它们。要解决此问题,您需要添加xml属性

android:descendantFocusability="afterDescendants"

到xml中的相对布局。

答案 1 :(得分:0)

问题来自动画,因为它只移动了imageView而不是它的真实位置。 我改为使用ObjectAnimator:)