如何打开一个网站时,一个按钮被点击的Android应用程序? [英] How to open a website when a Button is clicked in Android application?
问题描述
我设计一个应用程序,有几个按钮,用户点击。一旦点击按钮,用户被定向到合适的网站。我如何做到这一点?
I am designing an app, with several button for users to click on. Once button is clicked, user is directed to appropriate website. How do I accomplish this?
推荐答案
如果你是在谈论一个RCP应用程序,那么你需要什么是SWT 链接
小部件。
If you are talking about an RCP app, then what you need is the SWT link
widget.
<一个href="http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet183.java?view=co">Here是官方链接事件处理程序代码段。
Here is the official link event handler snippet.
更新
下面是极简主义的Android应用程序连接到超级用户或计算器,2个按钮。
Here is minimalist android application to connect to either superuser or stackoverflow with 2 buttons.
package ap.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class LinkButtons extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void goToSo (View view) {
goToUrl ( "http://stackoverflow.com/");
}
public void goToSu (View view) {
goToUrl ( "http://superuser.com/");
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
和这里的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/select" />
<Button android:layout_height="wrap_content" android:clickable="true" android:autoLink="web" android:cursorVisible="true" android:layout_width="match_parent" android:id="@+id/button_so" android:text="StackOverflow" android:linksClickable="true" android:onClick="goToSo"></Button>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="SuperUser" android:autoLink="web" android:clickable="true" android:id="@+id/button_su" android:onClick="goToSu"></Button>
</LinearLayout>
如果时间允许,我会添加网站标识的按钮,使之性感一点; - )
Time permitting, I'll add the site logos to the buttons to make it a bit sexier ;-)
这篇关于如何打开一个网站时,一个按钮被点击的Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!