【问题标题】:How to create Android application with button that goes to website? [duplicate]如何使用转到网站的按钮创建 Android 应用程序? [复制]
【发布时间】:2012-08-02 23:23:45
【问题描述】:

可能重复:
How to open a website when a Button is clicked in Android application?

所以我是新手,我已经搜索过,但似乎所有答案都是旧的,所以请不要只是将我链接到另一个线程。因为我正在学习,所以也要尽可能地描述!

问题: 如何使用 android sdk 在 eclipse 上创建一个 android 应用程序,该应用程序有一个按钮可以打开浏览器并转到特定站点。谢谢

-技术

【问题讨论】:

  • 做更好的研究,不要告诉人们不要链接答案。 stackoverflow.com/a/5026626/1067211该链接是一年半前的,应该仍然完全有效。
  • 我确实做到了,但没有成功......当然必须有更简单的方法来做到这一点......

标签: android url button browser android-intent


【解决方案1】:

您的应用程序需要两个主要部分,一个布局 xml 文件和一个 Java Activity。 xml 将包含一个父布局和一个按钮。 Java 文件会将内容视图设置为 xml 文件中包含的布局,提取对 Button 对象的引用并在其上设置一个单击侦听器,以便在用户按下它时获得回调。在回调中,您将使用 Intent 启动浏览器并打开给定的 url。

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"
    >

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/webBtn" />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity{
    Button webBtn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main);
        webBtn = (Button) findViewById(R.id.webBtn);
        webBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            /**************************
             * This Intent will tell the System
             * that we want to view something
             * in this case we are telling it that
             * we want to view the URL that we 
             * pass as the second parameter. 
             * it will search thru the applications
             * on the device and find the one(s)
             * that can display this type of content.
             * If there are multiples it will prompt
             * the user to choose which one they'd like
             * to use.
             ***************************/
            Intent i = new Intent(Intent.ACTION_VIEW, "https://www.google.com");
            startActivity(i);
        }
        });
    }
}

【讨论】:

    【解决方案2】:

    你需要做更多的研究。我知道你正在学习(我们都是从某个地方开始的,对吗?),但这并不意味着它可以让你不做作业。 @Tim's answer应该这样做,但这里有更多资源可以帮助您,不仅可以解决这个问题,还可以解决您可能遇到的更多问题: -Android 书籍:请参阅此question 以及此one。 -Android开发站:http://developer.android.com/index.html -这个网站 -当然还有 youtube 我知道这并不能直接回答您的问题,但希望它可以帮助您自己找到答案(加上评论太长了)!

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 2015-01-06
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多