Android设计支持库现在包括对Snackbar的支持。
我使用以下代码创建了一个:
string myTaskName = "Task";
foreach (var cur in BackgroundTaskRegistration.AllTasks)
if (cur.Value.Name == myTaskName)
{
return;
}
await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder
{
Name = "Task",
TaskEntryPoint = "Background.Task"
};
taskBuilder.SetTrigger(new TimeTrigger(15, true));
BackgroundTaskRegistration myFirstTask = taskBuilder.Register();
小吃吧可以通过轻扫解除。但是,我也想使用自己的Action Button(使用setAction函数创建)来解除它。
然而,似乎没有任何可用的功能可以做到这一点。
答案 0 :(得分:122)
对于Java,
.make
方法返回Snackbar
个对象。通过将该对象设为final
来保存该对象的实例。然后,在onClick()
中,拨打.dismiss
:
final Snackbar snackBar = Snackbar.make(findViewById(android.R.id.content), "Snackbar Message", Snackbar.LENGTH_LONG);
snackBar.setAction("Action Message", new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call your action method here
snackBar.dismiss();
}
});
snackBar.show();
对于Kotlin,
Snackbar.make(
findViewById(android.R.id.content),
"Snackbar Message",
Snackbar.LENGTH_INDEFINITE
).setAction("Action Message") { // Call action functions here }.show()
答案 1 :(得分:46)
实施点击操作并将其清空。点击空单击操作将关闭小吃栏。
Snackbar.make(coordinatorLayoutView, "Service Enabled", Snackbar.LENGTH_LONG)
.setAction("DISMISS", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
})
.show();
答案 2 :(得分:9)
当您使用Snackbar.LENGTH_LONG
时,您不需要操作按钮进行解雇,第二次自动关闭。
您应该使用以下代码:
Snackbar snackbar = Snackbar.make(relativeLayout, "Your Message", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("dismiss", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
snackbar.show();
小心这一行:
Snackbar.LENGTH_INDEFINITE
答案 3 :(得分:5)
这是一个老问题,但我只想分享我自己在Snackbar上类似功能的经验。因此我们为我们的应用程序设计了一个设计,即应该无限期地显示零食栏,用户应该能够将其解雇..但它内部不应该有DISMISS按钮(谷歌不建议在小吃店内放弃解雇或取消操作) 。我们的小吃店不得不通过点击它来解雇。
为我们工作的唯一解决方案是最终的(我在这里使用retrolambda,但也可以使用标准的View.OnClickListener):
final Snackbar snack = ... /* create proper snackbar as alway */
snack.getView().setOnClickListener(v -> snack.dismiss());
注意 getView()在中间调用。
答案 4 :(得分:2)
我遇到了同样的问题。当我使用.dismiss()时,动画看起来不同,有两个问题:
查看Snackbar的原始Android源代码我找到了以下解决方案:
View snackbarView = snackbar.getView();
Button snackbarActionButton = (Button) snackbarView.findViewById(android.support.design.R.id.snackbar_action);
//snackbarActionButton.setSoundEffectsEnabled(false); // might be considered in order not to have a confusing sound because nothing was clicked by the user
然后,我会在snackBarActionButton
上调用.performClicksnackBarActionButton.performClick();
链接到Snackbar的Android源代码: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/design/src/android/support/design/widget/Snackbar.java
答案 5 :(得分:1)
Snackbar(来自'com.android.support:design:23.2.1')支持多种类型的解雇操作。您可以使用事件创建一个简单的过滤器,例如在此示例中:
Snackbar.make(view, wornMessage, Snackbar.LENGTH_LONG).setActionTextColor(context.getResources().getColor(R.color.primary))
.setCallback(new Snackbar.Callback() {
@Override
public void onShown(Snackbar snackbar) {
super.onShown(snackbar);
// when snackbar is showing
}
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
if (event != DISMISS_EVENT_ACTION) {
//will be true if user not click on Action button (for example: manual dismiss, dismiss by swipe
}
}
})
.setAction("Undo, view1 -> {
// if user click on Action button
}).show();
Snackbar的解雇类型:
/** Indicates that the Snackbar was dismissed via a swipe.*/
public static final int DISMISS_EVENT_SWIPE = 0;
/** Indicates that the Snackbar was dismissed via an action click.*/
public static final int DISMISS_EVENT_ACTION = 1;
/** Indicates that the Snackbar was dismissed via a timeout.*/
public static final int DISMISS_EVENT_TIMEOUT = 2;
/** Indicates that the Snackbar was dismissed via a call to {@link #dismiss()}.*/
public static final int DISMISS_EVENT_MANUAL = 3;
/** Indicates that the Snackbar was dismissed from a new Snackbar being shown.*/
public static final int DISMISS_EVENT_CONSECUTIVE = 4;
P.S。在示例代码中使用了lambda表达式(由RetroLambda提供)