微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有为 googlePayButton 调用回调方法

如何解决没有为 googlePayButton 调用回调方法

我在片段的 OnCreate() 中有一个用于 Google Pay 按钮的 setonClickListener()。我正在使用视图绑定来访问它。

override fun onCreate(savedInstanceState: Bundle?) {
        setHasOptionsMenu(true)
        super.onCreate(savedInstanceState)
        binding = AddJobScreenBinding.inflate(layoutInflater)
        paymentsClient =  PaymentsUtil.createPaymentsClient(activity as Activity)
        possiblyShowGooglePayButton()
        binding.include.googlePayButton.setonClickListener( { requestPayment() })

PayButton 包含在我的片段的 xml 中:

<include
    android:id="@+id/include"
    layout="@layout/googlepay_button"
    android:layout_width="378dp"
    android:layout_height="69dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

这是按钮本身:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="48sp"
    android:background="@drawable/googlepay_button_no_shadow_background"
    android:paddingTop="2sp"
    android:contentDescription="@string/googlepay_button_content_description">
    <LinearLayout
        android:duplicateParentState="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2"
        android:gravity="center_vertical"
        android:orientation="vertical">
        <ImageView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:scaleType="fitCenter"
            android:duplicateParentState="true"
            android:src="@drawable/googlepay_button_content"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/googlePayButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:duplicateParentState="true"
        android:scaleType="fitXY"
        android:src="@drawable/googlepay_button_overlay" />
</RelativeLayout>

当按钮被点击时,虽然回调方法 requestPayment 没有被调用。 这是为什么?

解决方法

您似乎将点击侦听器设置为按钮内部的 ImageView。尝试将其设置为按钮元素本身:

binding.include.googlePayButton.setOnClickListener( { requestPayment() })

给元素一个更具描述性的名称也会很有用(例如:@+id/googlePayButton)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。