gpt4 book ai didi

java - 打开浏览器到网页 Android App

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:10:33 24 4
gpt4 key购买 nike

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

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

主.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>

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

}
}

最佳答案

它很接近,但有些东西放错了地方或丢失了。下面的代码有效——我试图进行最少的必要改动。您可以将两个版本加载到类似 WinMerge 的内容中以查看到底发生了什么变化。

主.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>

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

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

关于java - 打开浏览器到网页 Android App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6166522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com