gpt4 book ai didi

android - 让按钮打开浏览器到特定 URL 的最简单方法

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:48 24 4
gpt4 key购买 nike

这是一个简单的应用程序,我想要通过浏览器启动特定 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>

最佳答案

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

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

你的 Activity 应该有这样的部分:

//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% 准确的语法,因为我只是自己写的,但你明白了。

关于android - 让按钮打开浏览器到特定 URL 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9932775/

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