优步请求按钮 onClickListener
Uber RequestButton onClickListener
我在我的项目中使用 Uber android-api。
单击 RequestButton 后,它会将用户导航到 Uber App,但我想在导航到 Uber 应用程序之前处理一些数据。因此,为此,我想要 RequestButton 的 onClickListener。
我已经尝试直接在 requestButton 的对象上提供 onClickListener 然后我得到了事件但它没有导航到 Uber App。
requestButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
}
});
如果您想处理 Uber SDK
无法处理的内容,您需要自定义 RequestButton
。
为了能够调用您自己的自定义代码在点击 RequestButton 之前,即它的 OnClickListener 回调方法被调用,您必须更改游乐设施内 RequestButton 的代码-android-sdk.
然后在您的 activity 中,您必须调用 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");
}
});
}
另一种方法是创建自定义 class:
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);
}
使用自定义 class 如果需要,您还可以设置自定义 preOnClickListener。请参阅上述提交中的 rideRequestButtonWithCustomSubclassAndPreOnClickListener 方法。
@bitek 走在正确的轨道上,但这里是覆盖 RideRequestButton 的完整解决方案
public class CustomUberButton extends RideRequestButton implements
View.OnClickListener {
private View.OnClickListener internalListener;
private View.OnClickListener preClickListener;
public CustomUberButton(Context context) {
super(context);
}
public CustomUberButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomUberButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setPreClickListener(OnClickListener preClickListener) {
this.preClickListener = preClickListener;
}
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
// redirect the listener to us but keep the original listener registered by the original button to reuse later.
internalListener = l;
super.setOnClickListener(this);
}
@Override
public void onClick(View view) {
preClickListener.onClick(view);
internalListener.onClick(view);
}
}
上面的class可以用在xml
<com.example.android.takemehome.uber.CustomUberButton
android:id="@+id/btn_uber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
uber:ub__style="black" />
和 activity 看起来像
mUberButton = (CustomUberButton) findViewById(R.id.btn_uber);
mUberButton.setPreClickListener(this);
在这种情况下,activity 也实现了 View.OnClickListener
我在我的项目中使用 Uber android-api。 单击 RequestButton 后,它会将用户导航到 Uber App,但我想在导航到 Uber 应用程序之前处理一些数据。因此,为此,我想要 RequestButton 的 onClickListener。
我已经尝试直接在 requestButton 的对象上提供 onClickListener 然后我得到了事件但它没有导航到 Uber App。
requestButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
}
});
如果您想处理 Uber SDK
无法处理的内容,您需要自定义 RequestButton
。
为了能够调用您自己的自定义代码在点击 RequestButton 之前,即它的 OnClickListener 回调方法被调用,您必须更改游乐设施内 RequestButton 的代码-android-sdk.
然后在您的 activity 中,您必须调用 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");
}
});
}
另一种方法是创建自定义 class:
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);
}
使用自定义 class 如果需要,您还可以设置自定义 preOnClickListener。请参阅上述提交中的 rideRequestButtonWithCustomSubclassAndPreOnClickListener 方法。
@bitek 走在正确的轨道上,但这里是覆盖 RideRequestButton 的完整解决方案
public class CustomUberButton extends RideRequestButton implements
View.OnClickListener {
private View.OnClickListener internalListener;
private View.OnClickListener preClickListener;
public CustomUberButton(Context context) {
super(context);
}
public CustomUberButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomUberButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setPreClickListener(OnClickListener preClickListener) {
this.preClickListener = preClickListener;
}
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
// redirect the listener to us but keep the original listener registered by the original button to reuse later.
internalListener = l;
super.setOnClickListener(this);
}
@Override
public void onClick(View view) {
preClickListener.onClick(view);
internalListener.onClick(view);
}
}
上面的class可以用在xml
<com.example.android.takemehome.uber.CustomUberButton
android:id="@+id/btn_uber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
uber:ub__style="black" />
和 activity 看起来像
mUberButton = (CustomUberButton) findViewById(R.id.btn_uber);
mUberButton.setPreClickListener(this);
在这种情况下,activity 也实现了 View.OnClickListener