【问题标题】:How to set an onClickListener on an included layout?如何在包含的布局上设置 onClickListener?
【发布时间】:2022-05-11 22:19:48
【问题描述】:

我有一个带有 ImageButton 的布局,它包含在其他几个布局中。

图像按钮布局:

call_cancelled.xml

<LinearLayout 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" >

<LinearLayout
    android:id="@+id/callEndLayout"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:height="70dip"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <ImageButton
        android:id="@+id/phoneEnd"
        android:layout_width="63dp"
        android:layout_height="63dp"
        android:paddingBottom="7dp"
        android:background="@drawable/phone_cancelled"  />

</LinearLayout>

我的包括:

  <include
   android:id="@+id/includeCallEnd0"
   android:layout_width="70dp"
   android:layout_height="70dp"
   android:layout_alignBottom="@+id/include"
   android:layout_alignParentRight="true"
   layout="@layout/call_cancelled" />

为此,我在按下时需要一个 onClickListener。 我试过了:

View endcall0 = (View) findViewById(R.id.includeCallEnd0);

 endcall0.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent callIntent = new Intent(Intent.ACTION_CALL_BUTTON);
            startActivity(callIntent);

            int id = viewFlipper.getDisplayedChild();

            if (id == 0) {
                hideSoftKeyboard();
            }
        }
     });

但它不起作用。 有人知道解决办法吗?

【问题讨论】:

  • It doesn't work 表示代码未编译或应用在运行时崩溃?
  • findViewById(R.id.includeCallEnd0).findViewById(R.id.phoneEnd) 替换findViewById(R.id.includeCallEnd0),它应该可以工作
  • 包含什么类型的小部件?它扩展了哪个类?

标签: android onclicklistener


【解决方案1】:

findViewById(R.id.includeCallEnd0) 替换为findViewById(R.id.includeCallEnd0).findViewById(R.id.phoneEnd),它应该可以工作

因为您想在 ImageButton 而不是整个布局上设置点击侦听器


使用以下函数设置一次 OnClickListener:

private static void applyListener(View child, OnClickListener listener) {
    if (child == null)
        return;

    if (child instanceof ViewGroup) {
        applyListener((ViewGroup) child, listener);
    }
    else if (child != null) {
        if(child.getId() == R.id.phoneEnd) {
            child.setOnClickListener(listener);
        }
    }
}

private static void applyListener(ViewGroup parent, OnClickListener listener) {
    for (int i = 0; i < parent.getChildCount(); i++) {
        View child = parent.getChildAt(i);
        if (child instanceof ViewGroup) {
            applyListener((ViewGroup) child, listener);
        } else {
            applyListener(child, listener);
        }
    }
}

使用applyListener(rootView, yourOnClickListener);

【讨论】:

  • 它有效。谢谢。现在我必须为每个单独的包含编写一个 onClickListener。有没有办法通过 1 个 onclickListener 访问所有包含?
  • 抱歉还有一个问题。什么是 rootView?
  • rootView 在这种情况下是任何父视图,任何父视图包含视图层次结构中的所有 ImageButton。通常,它是 xml 中的第一个视图,通常是包含其他所有内容的 LinearLayout 或 RelativeLayout 或 FrameLayout。
  • 这个答案真是天才!非常感谢谢里夫。
  • 如果我确实想将监听器放在整个布局上怎么办?
【解决方案2】:

你本来可以的

LinearLayout endcall0 = (LinearLayout) findViewById(R.id.includeCallEnd0); 

然后简单

ImageButton imgBtn = (ImageButton) endcall0.findViewById(R.id. phoneEnd); 
imgBtn.setOnClickListener(this);

【讨论】:

    【解决方案3】:

    尝试在 callEndLayout (LinearLayaout) 上设置你的 clickListener

    【讨论】:

      【解决方案4】:

      使用 Kotlin 非常简单::

      {appBar} 是包含视图

      {btnOrders} 是 appBar 布局内的按钮视图

      binding.appBar.btnOrders.setOnClickListener {
         startActivity(Intent(this, OrdersActivity::class.java))
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-29
        • 1970-01-01
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多