2012-03-29 92 views
3

这是一个简单的应用程序,我想让我通过浏览器启动一个特定的URL。你们能否给我一些信息让我们知道这件事,就像我说过的,我已经有了这个按钮,可以在我的应用程序中使用。下面的代码 - 还是让我”知道,如果你有什么事最简单的方法是让按钮打开浏览器到特定的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> 

回答

10

此行应打开内置的浏览器,使用指定的网址:

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 2012-03-29 21:54:23

+0

如果这是你想做的事情,并帮助你,你能接受它作为答案吗? – hundeva 2012-03-30 14:10:33

+0

添加到我的项目后,我无法编译/运行。我应该把它放在哪里? – user1301764 2012-03-30 17:50:52

0

简单的创建XML一个WebView中

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

以下是一个

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

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

我已经添加了顶部的代码到我的.XML文件 - 这里究竟Java代码走?在我的.java文件或其他地方。感谢您的帮助! – user1301764 2012-03-30 17:44:56

+0

你必须在my.java文件中使用java代码,并且需要使用wv中的java代码和 wv。使用loadURL(URL); 将此代码粘贴到 Button的onClick()函数中。 – MAC 2012-03-31 07:56:12

相关问题