打开特定网址的最简单方法是使用按钮打开浏览器。

4

我有一个简单的应用程序,我想让我制作的按钮通过浏览器启动特定的URL。你们能给我一些信息来帮助我实现吗?就像我说的,我已经在我的应用程序中添加了该按钮。这是代码 - 如果需要其他信息,请告诉我。

.java文件

package reseeveBeta.mpi.dcasey;

import android.app.Activity;
import android.os.Bundle;

public class ReseeveBetaActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);                
    }  

}

.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to Reseeve, tap register to begin account creation" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Register" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textMultiLine"
    android:text="If you already have and account, please login below" >

    <requestFocus />
</EditText>

</LinearLayout>
3个回答

16

这行代码应该会打开你的内置浏览器,并跳转到指定的URL:

    startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));

你的活动应该有以下部分:

//define class variables here
Button btn;

protected void onCreate(Bundle savedInstanceState)
{
    //some code of yours
    btn=(Button)findViewById(R.id.button1);
    btn.setOnClickListener(this);
    //more code of yours
}

//whatever else you have in your source code

public void onClick(View v)
{
    //handle the click events here, in this case open www.google.com with the default browser
    startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}

这可能不是100%准确的语法,因为我只是自己写的,但你能理解我的意思。


1
非常感谢。如果有任何问题,我会回报的--再次感谢! - user1301764
如果这是你想要做的事情,并且对你有帮助,你能接受它作为答案吗? - hundeva
我在将其添加到我的项目后无法编译/运行。它应该放在哪里? - user1301764
如果你想在点击按钮后打开浏览器,那么你应该先为按钮注册事件,例如btn.setOnClickListener(this); 然后在onClick(arg)方法中,你可以粘贴这行代码。显然,将www.google.com替换为你想要打开的网址。应该能够编译通过,没有任何错误。 - hundeva
那会是什么样子?我无法将我的按钮注册到事件中。 - user1301764
这是我认为的代码 -- 我错了吗?`Button b = (Button) findViewById(R.id.button1); startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com"))); ) } }` - user1301764

1

0

在xml中简单创建一个WebView

<WebView
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />

这是实现该功能的简单Java代码

String URL="www.gtumca.co.cc";
WebView wv=(WebView)findViewById(R.layout.web_view);

onClick()
{
    wv.loadUrl(URL);
}

我已经将顶部代码添加到我的.XML文件中 - Java代码应该放在哪里?是在我的.java文件中还是其他地方。感谢您的帮助! - user1301764
你必须在my.java文件中使用Java代码,并将wv.loadUrl(URL);粘贴到Button的onClick()函数中。 - MAC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接