OnClickListener不适用于可点击属性

时间:2011-08-07 09:18:38

标签: android onclicklistener android-relativelayout

所以,我的问题是OnClickListener在我将android:clickable="true"设置到我的课程中时无效。

这是MyClass xml代码:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:clickable="true">
...
...
</RelativeLayout>

MyClass.java:

public class MyClass extends RelativeLayout implements OnClickListener {
    public MyClass(Context context) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.book_item, this);
        setOnClickListener(this);
    }

    public void onClick(View v) {
        Log.v("TAG", "Hello");
    }
...
...
}

android:clickable设置为false时,它可以正常工作。我错了什么?

5 个答案:

答案 0 :(得分:19)

设置OnClickListener会自动将clickable属性设置为true。你展示的代码虽然令人困惑。我的理解是,您的MyClass视图是XML文件中显示的RelativeLayout的父级。

如果是这样,孩子RelativeLayout将首先获得触摸事件(因为它可点击),但由于没有点击监听器,因此无法对其进行任何操作。

只需从XML中删除clickable=true即可。

答案 1 :(得分:8)

当你这样做时:android:clickable="true"你禁用了onClickListener(它不合逻辑,但它就像......)。

因此将其设置为"false"或者只是从XML文件中删除此行;)

答案 2 :(得分:5)

为您的布局添加ID:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/yourId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:clickable="true">
</RelativeLayout>

然后在java代码中:

public class MyClass extends RelativeLayout implements OnClickListener {
    public MyClass(Context context) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.book_item, this);
        findViewById(R.id.yourId).setOnClickListener(this);
    }

    public void onClick(View v) {
        Log.v("TAG", "Hello");
    }
...
...
}

答案 3 :(得分:1)

 ((RelativeLayout)findViewById(R.id.yourId)).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                //code area ...
                Log.v("TAG", "Hello");

                return true;
            } else{
                return false;
            }
        }
    });

您也可以这样使用。

答案 4 :(得分:0)

如果只有很少的元素需要执行操作,则可以在对元素进行过滤之后,将OnClickListener设置为每个元素,而不是对整个类实施OnClickListener。

TextView textLogin = findViewById(R.id.textLogin);

textLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.i("click", "textLoginClicked.");
    }
});

否则,您应该声明要设置OnClickListener的元素,

textLogin.setOnClickListener(this);

然后您就可以使用

@Override
public void onClick(View view) {
    if (view.getId() == R.id.textLogin) {
        Log.i("Click", "Login clicked.");
    }
}