打开浏览器到网页Android App

时间:2011-05-29 07:40:18

标签: java android

尝试掌握Java和Android在单击按钮后打开用户浏览器的简单任务需要帮助。

过去两天我一直在做教程,虽然如果我只是捅了一下并得到了反馈,这可能会有所帮助。提前感谢您的帮助。

main.xml中:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bgimage2"> 
    >

<Button
android:id="@+id/goButton"
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/start"
android:layout_x="80px"
android:layout_y="21px"
>
</AbsoluteLayout>

GetURL.java:

package com.patriotsar;

import android.app.Activity;
import android.content.Intent;
import android.view.View.OnClickListener;

String url = "http://www.yahoo.com";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
i.setData(u);


public class patriosar extends Activity {

     private Button goButton;

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


        goButton.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v){
                try {
                      // Start the activity
                      startActivity(i);
                    } catch (ActivityNotFoundException e) {
                      // Raise on activity not found
                      Toast toast = Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
                    }
                  } 
        });

    }
}

2 个答案:

答案 0 :(得分:6)

它很接近,但有些东西在错误的地方或丢失了。以下代码有效 - 我尝试进行最低限度的必要更改。您可以将这两个版本加载到类似WinMerge的版本中,以查看确切的更改内容。

main.xml中:

<?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"
    android:background="@drawable/bgimage2"
    >

    <Button
        android:id="@+id/goButton"
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/start"
        android:layout_x="80px"
        android:layout_y="21px"
    ></Button>
</LinearLayout>

GetURL.java:

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
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;
import android.widget.Toast;

public class GetURL extends Activity {
    private Button goButton;
    String url = "http://www.yahoo.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    Uri u = Uri.parse(url);
    Context context = this;

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

        goButton = (Button)findViewById(R.id.goButton);
        goButton.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v){
                try {
                      // Start the activity
                        i.setData(u);
                      startActivity(i);
                    } catch (ActivityNotFoundException e) {
                      // Raise on activity not found
                      Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
                    }
                  } 
        });
    }
}

(当然,您还需要bgimage2.png中的/res/drawable/个文件和start中的/res/values/strings.xml字符串。

答案 1 :(得分:4)

简化你可以做到

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(“http://www.yahoo.com”)); startActivity(意向);