【发布时间】:2011-06-28 22:06:28
【问题描述】:
我正在设计一个应用程序,有几个按钮供用户点击。单击按钮后,用户将被定向到适当的网站。我该如何做到这一点?
【问题讨论】:
-
当你说“我正在设计一个应用程序”时,你是指一个 RCP 应用程序,还是一个不基于 RCP 而是基于 IDE 本身的 eclipse 应用程序?
标签: android url button browser android-intent
我正在设计一个应用程序,有几个按钮供用户点击。单击按钮后,用户将被定向到适当的网站。我该如何做到这一点?
【问题讨论】:
标签: android url button browser android-intent
如果您谈论的是 RCP 应用程序,那么您需要的是 SWT link 小部件。
Here是官方的链接事件处理器sn-p。
更新
这是一个极简的 android 应用程序,通过 2 个按钮连接到超级用户或 stackoverflow。
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>
【讨论】:
在您的 Java 文件中编写以下代码...
ImageView Button = (ImageView)findViewById(R.id.yourButtonsId);
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.yourURL.com"));
startActivity(intent);
}
});
【讨论】:
这是一个可行的答案。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.todolist"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3"></uses-sdk>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".todolist"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.tutorial.todolist;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class todolist extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_clickme);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://www.anddev.org"));
startActivity(myWebLink);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/btn_clickme"
android:text="Click me..."
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
【讨论】:
导入
import android.net.Uri;
Intent openURL = new Intent(android.content.Intent.ACTION_VIEW);
openURL.setData(Uri.parse("http://www.example.com"));
startActivity(openURL);
或者可以使用,
TextView textView = (TextView)findViewById(R.id.yourID);
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.typeyourURL.com"));
startActivity(intent);
} });
【讨论】:
ImageView Button = (ImageView)findViewById(R.id.button);
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://google.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
【讨论】:
将此添加到按钮的点击监听器中:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
try {
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (ActivityNotFoundException exception) {
Toast.makeText(getContext(), "Error text", Toast.LENGTH_SHORT).show();
}
如果您将网站 url 作为变量而不是硬编码字符串,那么不要忘记处理 ActivityNotFoundException 并显示错误。或者您可能会收到无效的 url,应用程序将简单地崩溃。 (传递随机字符串而不是 url 变量并自己查看)
【讨论】:
你可以在你的按钮点击活动中使用它
Intent webOpen = new Intent(android.content.Intent.ACTION_VIEW);
WebOpen.setData(Uri.parse("http://www.google.com"));
startActivity(myWebLink);
并将其导入您的代码
import android.net.Uri;
【讨论】:
我只需要一行就可以在我的应用中显示一个网站:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://match4app.com")));
【讨论】:
public class MainActivity extends Activity {
private WebView webView1;
Button google;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
google = (Button) findViewById(R.id.google);
google.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
webView1 = (WebView) findViewById(R.id.webView);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.loadUrl("http://www.google.co.in/");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
【讨论】:
您可以将按钮包装在指向相应网站的锚点中。
<a href="http://www.stackoverflow.com">
<input type="button" value="Button" />
</a>
<a href="http://www.stackoverflow.com">
<input type="button" value="Button" />
</a>
<a href="http://www.stackoverflow.com">
<input type="button" value="Button" />
</a>
当用户单击按钮(输入)时,他们将被定向到锚点的 href 属性中指定的目的地。
编辑:糟糕,我没有在问题标题中阅读“Eclipse”。我的错。
【讨论】: