如何让小吃栏可点击?



众所周知,我们可以将action文本设置为snackbar。但我想让整个小吃店clickable

为此,我试图使snackbarview可点击,但这是徒劳的。任何人都可以帮我解决这个问题吗?

法典:

Snackbar snackbar=Snackbar.make(home_btn, content, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
sbView.setClickable(true);
sbView.setFocusable(true);
sbView.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = sbView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.parseColor("#001919"));
CoordinatorLayout.LayoutParams params=(CoordinatorLayout.LayoutParams)sbView.getLayoutParams();
params.gravity = Gravity.TOP;
sbView.setLayoutParams(params);
snackbar.show();
sbView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.setAction(action);
i.putExtras(b);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
}
});

其实是我的错,我忘了开始intent.所以,最终的答案将是

Snackbar snackbar=Snackbar.make(home_btn, content, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
sbView.setClickable(true);
sbView.setFocusable(true);
sbView.setBackgroundColor(Color.parseColor("#ffffff"));
TextView tv = sbView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.parseColor("#001919"));
CoordinatorLayout.LayoutParams params=(CoordinatorLayout.LayoutParams)sbView.getLayoutParams();
params.gravity = Gravity.TOP;
sbView.setLayoutParams(params);
snackbar.show();
sbView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.setAction(action);
i.putExtras(b);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity(i);
}
});

我有一个使用 Kotlin 扩展的替代方案。

将以下 2 个扩展复制到代码中:

此扩展用于制作可链接的文本视图:

// Will set links on a text view
fun TextView.setLinks(vararg links: Pair<String, View.OnClickListener>) {
val spannableString = SpannableString(this.text)
for (link in links) {
val clickableSpan = object : ClickableSpan() {
override fun updateDrawState(textPaint: TextPaint) {
// use this to change the link color
textPaint.color = textPaint.linkColor
// toggle below value to enable/disable
// the underline shown below the clickable text
textPaint.isUnderlineText = true
}
override fun onClick(view: View) {
Selection.setSelection((view as TextView).text as Spannable, 0)
view.invalidate()
link.second.onClick(view)
}
}
val startIndexOfLink = this.text.toString().indexOf(link.first)
spannableString.setSpan(
clickableSpan, startIndexOfLink, startIndexOfLink + link.first.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
this.movementMethod = LinkMovementMethod.getInstance() // without LinkMovementMethod, link can not click
this.setText(spannableString, TextView.BufferType.SPANNABLE)
}

此扩展是从活动中弹出小吃店:

// Will pop a snack bar from an activity
fun Activity.popSnackBar(text: String, duration: Int = Snackbar.LENGTH_LONG, links: Array<Pair<String, View.OnClickListener>>? = null) {
val sb = Snackbar.make(findViewById<View>(android.R.id.content).rootView, text, duration)
if (links != null) {
val tv = sb.view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
tv.setLinks(*links)
}
sb.show()
}

最后,从您的活动中调用它:

popSnackBar("Click here or exit", links = arrayOf(
Pair("here", View.OnClickListener { println("clicked here") }),
Pair("exit", View.OnClickListener { println("clicked exit") })
))

相关内容

  • 没有找到相关文章

最新更新