当前位置: 代码迷 >> Android >> 如何将动作添加到字符串android的一部分
  详细解决方案

如何将动作添加到字符串android的一部分

热度:84   发布时间:2023-08-04 11:04:47.0

我有一个休闲的文字:“单击确定,您将禁用该服务。了解更多”。

我想单击“了解更多”,但是我希望出现一个弹出菜单,而不是直接转到网站

我用过休闲堆栈问题:

效果很好。 我发现通过“。”了解更多信息的索引。 该解决方案使中文和印地语语言的应用程序崩溃(在印地语中,点写为-> |)。

如何以通用方式使“了解更多”可单击以显示弹出菜单?

也许有一种方法可以在strings.xml中定义点击动作,例如调用链接吗? (而不是调用链接->启动弹出菜单?)

您可以使用WebView和锚点。 创建新的WebViewClient (尤其是您需要此方法: shouldOverrideUrlLoading() ),并在用户单击锚点时执行所需的所有操作。

您可以根据定义的文本创建单击事件。.检查此库..可能会帮助您..

解决了它,可能是一个hack,但是效果很好。

在我添加的strings.xml中

//<a></a> tags to be removed later on
<string name="learn_more">By clicking OK you will disable the service. &#60;a&#62;Learn more&#60;&#47;a&#62;</string>

在代码中:

TextView textView= (TextView) findViewById(R.id.textViewInLayout);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(R.string.learn_more);

//indexes of the clickable text
int start = textView.getText().toString().indexOf("<a>");
int end = textView.getText().toString().indexOf("</a>");

//set the text as html to make the tags disappear 
textView.setText(Html.fromHtml(getString(R.string.learn_more)));

//make the text clickable
Spannable spannable = (Spannable) textView.getText();

ClickableSpan myClickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
       yourActionHere();
  }
};

// end - 3 beacuse of </a>
spannable.setSpan(myClickableSpan, start, end - 3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);`
  相关解决方案