[英]How to place a Market link inside an App?
In my app I want to have something like "Click HERE" where 'HERE' is a link to the app in the market. 在我的应用程序中,我希望有一些类似“点击此处”的内容,其中“HERE”是市场中应用程序的链接。
So I've done: 所以我做到了:
TextView link = (TextView) findViewById(R.id.appbrain);
link.setText("market://details?id=com.appspot.swisscodemonkeys.apps&hl=pt_PT");
Linkify.addLinks(link, Linkify.ALL);
But this does not work. 但这不起作用。 How should I do?
我应该怎么做?
Use the web version of the link: http://market.android.com/details?id=<package name>
. 使用链接的Web版本:
http://market.android.com/details?id=<package name>
: http://market.android.com/details?id=<package name>
。 Users will either be prompted whether they wish to open the link in the Browser or Market app, or if they have already chosen a default it will open right away. 将提示用户是否希望在浏览器或市场应用程序中打开链接,或者如果他们已经选择了默认值,它将立即打开。
You could also use some HTML and set the text of the TextView like this: 您还可以使用一些HTML并设置TextView的文本,如下所示:
textView.setText(Html.fromHtml("<a href=\\"market://details?id=com.appspot.swisscodemonkeys.apps&hl=pt_PT\\">CLICK HERE</a>"));
You just have to define a pattern, as this URL is the custom defined for the market application. 您只需定义一个模式,因为此URL是为市场应用程序定义的自定义。
Pattern appWordMatcher = Pattern.compile("\\b[A-Z]+[a-z0-9]+[A-Z][A-Za-z0-9]+\\b");
String appMarketURL = "market://details?id=com.appspot.swisscodemonkeys.apps&hl=pt_PT";
Linkify.addLinks(txtView, appWordMatcher, appMarketURL);
For more information see WikiNotes: Linkify your Text! 有关更多信息,请参阅WikiNotes:链接您的文本!
Ok, both answers are partially right; 好的,两个答案都是正确的; the problem of both is that links are not clickable.
两者的问题是链接不可点击。 However, I found the problem.
但是,我发现了这个问题。
When we want to linkify URLs (directly) we just have to do: 当我们想要(直接)链接URL时,我们必须这样做:
TextView link = (TextView) findViewById(R.id.url);
link.setText("http://google.pt");
Linkify.addLinks(link, Linkify.ALL);
But when we want to "play" with the link we need to use (as stated in another answer) we could use HTML: 但是当我们想要“玩”我们需要使用的链接时(如另一个答案所述)我们可以使用HTML:
TextView link = (TextView) findViewById(R.id.appbrain);
link.setText(Html.fromHtml("" +
"- Download <a href=\"https://market.android.com/details?id=com.appspot.swisscodemonkeys.apps\">Appbrain</a>" +
" to update all your apps!"));
Even if we use the following code, the link will not be clickable. 即使我们使用以下代码,该链接也不会被点击。
Linkify.addLinks(link, Linkify.ALL);
The solution is just using the following code: 解决方案只是使用以下代码:
link.setMovementMethod(LinkMovementMethod.getInstance());
Another option is to just make a button or clickable textfield and launch an activity from there. 另一个选择是只创建一个按钮或可单击的文本字段,然后从那里启动一个活动。
try
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.mypackage.myapp")));
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Cannot load Play Store", Toast.LENGTH_SHORT).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.