如何使用进入网站的按钮创建Android应用程序?

时间:2012-08-05 22:40:43

标签: android url button browser android-intent

  

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

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

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

-Techno

2 个答案:

答案 0 :(得分:0)

您的应用程序需要两个主要部分:布局xml文件和Java活动。 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);
        }
        });
    }
}

答案 1 :(得分:0)

你需要做更多的研究。我知道你正在学习(我们都在某个地方开始了吗?),但这并不意味着它会给你一个不做功课的通行证。 @Tim的答案应该这样做但是这里有一些更多的资源可以帮助你解决这个问题,但是你可能会遇到更多的问题: -Android书籍:请参阅此question以及此one。 -Android开发网站:http://developer.android.com/index.html - 这个网站 - 当然是youtube 我知道这并没有直接回答你的问题,但希望它会帮助你自己找到答案(再加上很长时间才能适应评论)!