如何使小吃店可点击?

How to make snackbar clickable?

据我们所知,我们可以将 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)
}

此扩展用于从 activity 弹出小吃店:

// 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()
}

最后,从您的 activity 中调用它:

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