使用单击侦听器包含TAG

问题描述 投票:0回答:2

我有一个包含两个包含外部布局的片段:

        <include
        android:id="@+id/buttonSelectDate"
        layout="@layout/content_dateinstall_datebutton"
        android:layout_width="141dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginStart="38dp"
        android:layout_marginTop="35.9dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtInsertPhoneNumber"/>

    <include
        android:id="@+id/buttonSelectPeriod"
        layout="@layout/content_dateinstall_periodbutton"
        android:layout_width="141dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginEnd="38dp"
        android:layout_marginTop="35.9dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtInsertPhoneNumber"/>

我想设置OnClick方法,因为我将使用该布局作为按钮。

当我设置View的onclicklistener应用程序在运行时崩溃。我一直在尝试使用@BindView和@OnClick通过ButterKnife绑定onClick,并以常用方式创建对象并设置onClickListener。

任何人都知道我该怎么做?

这是我的buttonLayout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <variable name="orderViewModel" type="com.oi.pap.viewmodels.OrderViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/linearLayout2"
    android:layout_width="141dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/custom_border_button">

    <TextView
        style="@style/TextAppearance.simplonRegular"
        android:id="@+id/txtSelectPeriod"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/all_period"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="11dp"
        android:textSize="12sp"
        android:textColor="@color/lipstick"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <TextView
        style="@style/TextAppearance.simplonHeadline"
        android:id="@+id/txtSelectPeriodValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@={orderViewModel.periodInstallValue}"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="11dp"
        android:layout_marginBottom="10dp"
        android:textSize="14sp"
        android:textColor="@color/black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtSelectPeriod"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

android button include click fragment
2个回答
0
投票

从我看到你尝试从主布局访问到包含布局,你正在使用MVVM方法,当你尝试访问内部的主要问题包括布局,它无法获得已声明的数据是什么,所以你必须在主布局中声明它然后将它传递给include布局,例如:在我的LoginLayout.xml中:

<data>
    <variable
        name="activityLogin"
        type="...LoginViewModel"/>
</data>

在这里我想要从活动登录然后在include中获取数据:

<include
    ....
    bind:contentLogin="@{activityLogin}"
    ..../>

在内容登录:

<data>
        <variable
            name="contentLogin"
            type="...LoginViewModel"/>
    </data>

然后,就是你从你的点击中获得数据


0
投票

我发现。

private FragmentDateinstallHomeBinding dataBinding;
dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_dateinstall_home, 
container, false);
View view = dataBinding.getRoot();
//here
dataBinding.buttonSelectDate.dateInstallButtonLayout.setOnClickListener(tst);
© www.soinside.com 2019 - 2024. All rights reserved.