Android根视图onClickListener总是被触发

Android根视图onClickListener总是被触发

本文介绍了Android根视图onClickListener总是被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android中的视图有一个简单的问题.我有两个视图rootView和containerView,containerView包含在rootView中,但是我不明白为什么当我单击containerView时会触发rootView.

I have a simple problem with views in Android. I have two view rootView and containerView, containerView is contained in rootView, but I don't understand why when I click on containerView rootView is triggered.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    LinearLayout rootView = new LinearLayout(this);
    rootView.setBackgroundColor(Color.BLUE);
    rootView.setGravity(Gravity.CENTER);
    rootView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
    rootView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("esx","ROOT VIEW ID: "+v.getId());
        }
    });

    LinearLayout containerView = new LinearLayout(this);
    containerView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100));
    containerView.setBackgroundColor(Color.CYAN);

    rootView.addView(containerView);

    setContentView(rootView);
}}

我希望当我单击containerView时,什么都不会发生.有办法吗?

I want that when I click containerView, nothing happens. Is there a way to do this?

谢谢

推荐答案

尝试设置 containerView.setClickable(true)

这篇关于Android根视图onClickListener总是被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:55