[英]Why is my Android snackbar dismissed after clicking its action button?
I have a snackbar that I build with a duration set to Snackbar.LENGTH_INDEFINITE
我有一个构建的快餐栏,其持续时间设置为
Snackbar.LENGTH_INDEFINITE
The snackbar is displayed properly when I call mySnackbar.show();
当我调用
mySnackbar.show();
时,小吃栏会正确显示mySnackbar.show();
But as soon as I hit the action button, the snackbar is dismissed. 但是,一旦我按下“动作”按钮,小吃店就被解雇了。
The dismiss method seems called by the system. dismiss方法似乎由系统调用。
Does anyone know a workaround ? 有谁知道解决方法?
Here is my code for building my snackbar: 这是构建小吃店的代码:
Snackbar mySnackbar = Snackbar.make(mParent, R.string.the_question, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.yes, new View.OnClickListener() {
@Override
public void onClick(View v) {
//My code...
}
})
.addCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
}
@Override
public void onShown(Snackbar snackbar) {
}
});
Below code is showing the Alert dialog "after" the snackbar is displayed. 下面的代码显示了显示小吃栏“之后”的“警报”对话框。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
"This is Snackbar", Snackbar.LENGTH_INDEFINITE).
setAction(R.string.yes, new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}).addCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
super.onDismissed(transientBottomBar, event);
}
@Override
public void onShown(Snackbar sb) {
super.onShown(sb);
}
});
snackbar.show();
showAlertDialog(this, "Alert!!", "Alert Dialog", "Yes", "No");
}
The showAlertDialog is simple static method to show the dialog showAlertDialog是显示对话框的简单静态方法
public static void showAlertDialog(Context context, String title, String message, String posBtnMsg, String negBtnMsg) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(posBtnMsg, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton(negBtnMsg, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
The screen shot of the output for above code is below, 上面的代码输出的屏幕截图如下,
The answer to this question lies in the way Snackbar.setAction(CharSequence text, final View.OnClickListener listener)
is implemented 这个问题的答案在于
Snackbar.setAction(CharSequence text, final View.OnClickListener listener)
的实现方式
If you pass this method a non empty text or non null listener, the TextView displaying the action's text is set an OnClickListener which calls BaseTransientBottomBar.dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION)
when the action is performed. 如果将此方法传递为非空文本或非null侦听器,则将显示操作文本的TextView设置为OnClickListener,该
BaseTransientBottomBar.dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION)
在执行操作时调用BaseTransientBottomBar.dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION)
。 This causes the Snackbar to be dismissed. 这将导致小吃店被解散。
To prevent that, one needs to retrieve the TextView of the Snackbar's action view, and override its OnClickListener with a listener that does not call dispatchDismiss() 为防止这种情况,需要检索Snackbar动作视图的TextView,并使用不调用dispatchDismiss()的侦听器覆盖其OnClickListener。
Here is the Snackbar.setAction()
code for reference 这是
Snackbar.setAction()
代码供参考
public Snackbar setAction(CharSequence text, final View.OnClickListener listener) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) mView.getChildAt(0);
final TextView tv = contentLayout.getActionView();
if (TextUtils.isEmpty(text) || listener == null) {
tv.setVisibility(View.GONE);
tv.setOnClickListener(null);
} else {
tv.setVisibility(View.VISIBLE);
tv.setText(text);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClick(view);
// Now dismiss the Snackbar
dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION);
}
});
}
return this;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.