是否可以在按钮中嵌入HTML链接?
我有此广告资源:
<a href="https://www.financeads.net/tc.php?t=29425C87039197T" target="_blank">Hier klicken</a><img src="https://www.financeads.net/tb.php?t=29425V87039197T" alt="" border="0" width="0" height="0">
会员网络告诉我,我必须使用HTML链接,而不能仅使用URL。 我想在android中显示一个按钮。当您单击按钮时,URL应该打开。是否可以将此HTML嵌入Android按钮中?
答案 0 :(得分:0)
您可以尝试setMovementMethod
方法
Button btn_text;
btn_text = (Button) findViewById(R.id.btn_text);
Spanned html = Html.fromHtml("<a href=\"https://www.financeads.net/tc.php?t=29425C87039197T\" target=\"_blank\">Hier klicken</a><img src=\"https://www.financeads.net/tb.php?t=29425V87039197T\" alt=\"\" border=\"0\" width=\"0\" height=\"0\">");
/*
setMovementMethod(MovementMethod movement)
Sets the movement method (arrow key handler) to be used for this Button.
LinkMovementMethod
A movement method that traverses links in the text buffer and
scrolls if necessary. Supports clicking on links with
DPad Center or Enter.
*/
btn_text.setMovementMethod(LinkMovementMethod.getInstance());
// Set Button text from html
btn_text.setText(html);
Imp:确保已在android清单文件中设置了Internet权限
答案 1 :(得分:0)
将html链接嵌入按钮有什么意义?我会通过执行以下操作来覆盖对按钮的点击:
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
String url = "https://www.financeads.net/tc.php?t=29425C87039197T";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
通过这种方式,您将导航到浏览器手机提供的链接