有没有办法在高频直播卡中使用GDK的Slider.GracePeriod
?我有菜单选项“发送消息”,点击它并输入你的消息后,我想用宽限期滑块显示一个“发送消息...”警告对话框(或类似的东西)。但是,我没有成功获得宽限期滑块出现在对话框中(即使用传递给Dialog.setContentView()
的相同视图作为Slider.from()
的参数),实时卡菜单活动或其他任何地方。有什么想法吗?
(这很可能与此无关,但我确实注意到Google的GDK参考提到宽限期滑块在“timeInMs”中给定的宽限期内动画。这听起来像是函数或构造函数的参数,但是如果所以我找不到一个。如果你没有设置它,也许宽限期滑块不起作用?)
注意:我知道this现有的问题,但它还没有得到解答,它涉及滑块和真卡的不同类型,所以另一个问题似乎是合适的。
答案 0 :(得分:1)
以下内容适用于您;以下类包含宽限期/确认对话框逻辑,以提供与内置Glassware相同的体验。
public class GracePeriodDialog extends Dialog {
private static final long COMPLETION_MESSAGE_TIMEOUT_MILLIS = 1000;
private final DialogInterface.OnClickListener mOnClickListener;
private final AudioManager mAudioManager;
private final GestureDetector mGestureDetector;
private final CharSequence mCompletedText;
private View mContentView;
private Slider.GracePeriod mGracePeriod;
/** Handles the tap gesture to complete the grace period early. */
private final GestureDetector.BaseListener mBaseListener =
new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
// Cancel the grace period if the user tapped early so that the completion event
// doesn't fire twice.
mGracePeriod.cancel();
showCompletion();
return true;
}
return false;
}
};
/** Called when the grace period has ended to show the completion message. */
private final Slider.GracePeriod.Listener mGracePeriodListener =
new Slider.GracePeriod.Listener() {
@Override
public void onGracePeriodEnd() {
showCompletion();
}
@Override
public void onGracePeriodCancel() {}
};
/**
* Called after a 1-second delay when the grace period has completed (or the user tapped).
* Forwards the successful completion as an {@code onClick} event on the dialog.
*/
private final Runnable mDoneRunnable = new Runnable() {
@Override
public void run() {
if (mOnClickListener != null) {
// Since Glass dialogs do not have buttons, the index passed to onClick is always 0.
mOnClickListener.onClick(GracePeriodDialog.this, 0);
}
dismiss();
}
};
/**
* Creates a new {@code GracePeriodDialog}. The dialog can show a progress message and icon
* while the grace period is running, which changes to a completion message when the grace
* period ends (for example, "Sending" -> "Sent".
*/
public GracePeriodDialog(Context context,
int progressIconId, CharSequence progressText,
CharSequence completedText,
DialogInterface.OnClickListener onClickListener) {
super(context);
mOnClickListener = onClickListener;
mAudioManager =
(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mGestureDetector =
new GestureDetector(context).setBaseListener(mBaseListener);
mCompletedText = completedText;
updateContentView(progressText, progressIconId);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mGracePeriod = Slider.from(mContentView).startGracePeriod(mGracePeriodListener);
}
@Override
public void onBackPressed() {
mGracePeriod.cancel();
super.onBackPressed();
}
/** Overridden to let the gesture detector handle a possible tap event. */
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
return mGestureDetector.onMotionEvent(event)
|| super.onGenericMotionEvent(event);
}
private void showCompletion() {
mAudioManager.playSoundEffect(Sounds.SUCCESS);
updateContentView(mCompletedText, R.drawable.ic_done_50);
mContentView.postDelayed(mDoneRunnable, COMPLETION_MESSAGE_TIMEOUT_MILLIS);
}
private void updateContentView(CharSequence text, int iconResId) {
mContentView = new CardBuilder(getContext(), CardBuilder.Layout.MENU)
.setText(text)
.setIcon(iconResId)
.getView();
setContentView(mContentView);
}
}
可能需要注意的一点是:在显示对话框或开始宽限期之前,请确保您的菜单活动没有完成(例如,在onOptionsMenuClosed
中)。