当来自 belonging to this library 的任何按钮时,我想成功运行下面的代码 ( this Rating Dialog )被按下。我该怎么做?
public void goToPickerActivity() {
Intent intent = new com.sucho.placepicker.PlacePicker.IntentBuilder()
.onlyCoordinates(true) //Get only Coordinates from Place Picker
.showLatLong(true)
.setLatLong(40.71274, -74.005974) // Initial Latitude and Longitude the Map will load into (NYC coordinates)
.setMapZoom(2.5f) // Map Zoom Level. Default: 14.0
.build(this);
startActivityForResult(intent, Constants.PLACE_PICKER_REQUEST);
}
编辑:我想执行 goToPickerActivity()
第一个 然后执行库的预期点击代码。例如,当 Rate App
单击按钮:第一步是:执行
goToPickerActivity()
.完成后,第 2 步是:运行通常的
Rate App
点击代码。
最佳答案
您可以尝试添加 OnRatingDialogListener
使用 setRateButtonText
, setRateLaterButtonText
或 setNeverRateButtonText
AppRatingDialog.Builder
的方法类(class):
final AppRatingDialog appRatingDialog = new AppRatingDialog.Builder(this)
// ... call another setup methods if need
.setRateButtonText("Rate App", new OnRatingDialogListener() {
@Override
public void onClick() {
goToPickerActivity();
}
})
.setRateLaterButtonText("Rate App Later", new OnRatingDialogListener() {
@Override
public void onClick() {
goToPickerActivity();
}
})
.setNeverRateButtonText("Never", new OnRatingDialogListener() {
@Override
public void onClick() {
goToPickerActivity();
}
})
.build();
appRatingDialog.show();
使用 lambda 将如下所示:final AppRatingDialog appRatingDialog = new AppRatingDialog.Builder(this)
// ... call another setup methods if need
.setRateButtonText("Rate App", () -> goToPickerActivity())
.setRateLaterButtonText("Rate App Later", () -> goToPickerActivity())
.setNeverRateButtonText("Never", () -> goToPickerActivity())
.build();
appRatingDialog.show();
编辑1:方法
openPlayStore
:private void openPlayStore() {
final String storeLink = "market://details?id=" + context.getPackageName();
final Uri marketUri = Uri.parse(storeLink);
try {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(mContext, "Couldn't find PlayStore on this device", Toast.LENGTH_SHORT).show();
}
}
如果您可以编辑库,请尝试制作 openPlayStore
方法public
, 保存对 AppRatingDialog
的引用实例化一些变量,例如appRatingDialog
并调用appRatingDialog.openPlayStore()
.编辑2:
要为按钮设置不同的监听器,我们需要向
AppRatingDialog
添加新的监听器。并处理它们:public class AppRatingDialog extends AppCompatDialog implements View.OnClickListener {
//...
private OnRatingDialogListener onRatingDialogListener;
private OnRatingDialogListener onRateLaterDialogListener;
private OnRatingDialogListener onRateNeverDialogListener;
//...
@Override
public void onClick(View v) {
if (v.getId() == R.id.dialog_rating_button_never_rate) {
savedNeverShow();
dismiss();
if (onRateNeverDialogListener != null) {
onRateNeverDialogListener.onClick();
}
} else if (v.getId() == R.id.dialog_rating_button_rate_later) {
dismiss();
incrementLaunchCount(true);
if (onRateLaterDialogListener != null) {
onRateLaterDialogListener.onClick();
}
} else if (v.getId() == R.id.dialog_rating_button_rate) {
savedNeverShow();
dismiss();
if (onRatingDialogListener != null) {
onRatingDialogListener.onClick();
} else {
openPlayStore();
}
}
}
// ...
public static class Builder {
//...
@NonNull
public Builder setRateLaterButtonText(String rateLaterButtonText, @Nullable OnRatingDialogListener onRateLaterClickListener) {
appRatingDialog.mRateLaterButtonText = rateLaterButtonText;
appRatingDialog.onRateLaterDialogListener = onRateLaterClickListener;
return this;
}
@NonNull
public Builder setNeverRateButtonText(String neverRateButtonText, @Nullable OnRatingDialogListener onNeverRateClickListener) {
appRatingDialog.mNeverRateButtonText = neverRateButtonText;
appRatingDialog.onRateNeverDialogListener = onNeverRateClickListener;
return this;
}
// ...
}
}
编辑 3:要同时打开 Play 商店并通知监听器:
public class AppRatingDialog extends AppCompatDialog implements View.OnClickListener {
//...
private OnRatingDialogListener onRatingDialogListener;
private OnRatingDialogListener onRateLaterDialogListener;
private OnRatingDialogListener onRateNeverDialogListener;
//...
@Override
public void onClick(View v) {
if (v.getId() == R.id.dialog_rating_button_never_rate) {
savedNeverShow();
dismiss();
if (onRateNeverDialogListener != null) {
onRateNeverDialogListener.onClick();
}
} else if (v.getId() == R.id.dialog_rating_button_rate_later) {
dismiss();
incrementLaunchCount(true);
if (onRateLaterDialogListener != null) {
onRateLaterDialogListener.onClick();
}
} else if (v.getId() == R.id.dialog_rating_button_rate) {
savedNeverShow();
dismiss();
openPlayStore();
if (onRatingDialogListener != null) {
onRatingDialogListener.onClick();
}
}
}
// ...
}
关于java - 按下对话框的按钮时打开另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61974587/