我在项目中使用Uber android-api
单击RequestButton时,它将导航用户到Uber应用程序,但是我想先处理一些数据,然后再导航到Uber应用程序。因此,我想要RequestButton的onClickListener。

我已经尝试过直接在requestButton的对象上提供onClickListener,然后得到事件,但它无法导航到Uber App。



requestButton.setOnClickListener(new View.OnClickListener()
{
      @Override
       public void onClick(View v) {

       }
});

最佳答案

为了能够在单击RequestButton之前调用自己的自定义代码,即调用了OnClickListener回调方法,您必须在rides-android-sdk中更改RequestButton的代码。

然后,在您的活动中,必须调用RequestButton对象上的setCustomPreOnClickListener:

public void rideRequestButtonWithCustomPreOnClickListener() {
        RequestButton uberButtonBlack = (RequestButton) findViewById(R.id.uber_button_black);
        RequestButton uberButtonWhite = (RequestButton) findViewById(R.id.uber_button_white);

        RideParameters rideParameters = new RideParameters.Builder()
                .setProductId(UBERX_PRODUCT_ID)
                .setPickupLocation(PICKUP_LAT, PICKUP_LONG, PICKUP_NICK, PICKUP_ADDR)
                .setDropoffLocation(DROPOFF_LAT, DROPOFF_LONG, DROPOFF_NICK, DROPOFF_ADDR)
                .build();

        uberButtonBlack.setRideParameters(rideParameters);
        uberButtonWhite.setRideParameters(rideParameters);

        uberButtonBlack.setCustomPreOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Custom code for black button called before UberRequestButton's onClick() listener");
            }
        });

        uberButtonWhite.setCustomPreOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Custom code for white button called before UberRequestButton's onClick() listener");
            }
        });
    }


另一种方法是创建一个自定义类:

package com.uber.sdk.android.rides.samples;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.uber.sdk.android.rides.RequestButton;

public class CustomUberRequestButton extends RequestButton implements View.OnClickListener {
    private static final String TAG = CustomUberRequestButton.class.getSimpleName();

    public CustomUberRequestButton(Context context) {
        super(context);
    }

    public CustomUberRequestButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomUberRequestButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG, "Custom code before calling UberRequestButton's onClick() listener");

        // Explicitly need to call UberRequestButton onClick() listener
        super.onClick(v);
    }
}


您可以像这样使用它:

/**
 * Please make sure you replace com.uber.sdk.android.rides.RequestButton
 * with com.uber.sdk.android.rides.samples.CustomUberRequestButton inside
 * the res/layout/activity_sample.xml file, otherwise you will get a ClassCastException.
 */
public void rideRequestButtonWithCustomSubclassOnClickListener() {
    CustomUberRequestButton uberButtonBlack = (CustomUberRequestButton) findViewById(R.id.uber_button_black);
    CustomUberRequestButton uberButtonWhite = (CustomUberRequestButton) findViewById(R.id.uber_button_white);

    RideParameters rideParameters = new RideParameters.Builder()
            .setProductId(UBERX_PRODUCT_ID)
            .setPickupLocation(PICKUP_LAT, PICKUP_LONG, PICKUP_NICK, PICKUP_ADDR)
            .setDropoffLocation(DROPOFF_LAT, DROPOFF_LONG, DROPOFF_NICK, DROPOFF_ADDR)
            .build();

    uberButtonBlack.setRideParameters(rideParameters);
    uberButtonWhite.setRideParameters(rideParameters);
}


如果需要,您还可以使用自定义类设置自定义preOnClickListener。在上述提交中,请参见rideRequestButtonWithCustomSubclassAndPreOnClickListener方法。

10-08 08:06