java 打开浏览器到网页 Android App

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6166522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:37:54  来源:igfitidea点击:

Open browser to web page Android App

javaandroid

提问by Denoteone

Trying to grasp Java and Android would like help with a simple task of opening a users browser after they click a button.

试图掌握 Java 和 Android 需要帮助完成一个简单的任务,即在用户单击按钮后打开浏览器。

I have been doing tutorials for the last two days though it might help if I just took a stab at it and got feedback. thanks in advance for any help.

过去两天我一直在做教程,但如果我只是尝试一下并获得反馈可能会有所帮助。提前感谢您的帮助。

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:

获取URL.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);
                    }
                  } 
        });

    }
}

回答by Sven Viking

It's close, but a few things are in the wrong place or missing. The below code works -- I tried to make the minimum necessary alterations. You could load both versions into something like WinMergeto see exactly what changed.

它很接近,但有些东西在错误的地方或丢失了。下面的代码有效——我试图进行最少的必要改动。您可以将两个版本都加载到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:

获取URL.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);
                    }
                  } 
        });
    }
}

(You also need a bgimage2.pngfile in /res/drawable/and a startstring in /res/values/strings.xml, of course).

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

回答by Dave S

To simplify you could do

为了简化你可以做

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com")); startActivity(intent);

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(" http://www.yahoo.com")); 开始活动(意图);