-1

可能重复:
在 Android 应用程序中单击按钮时如何打开网站?

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

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

-技术

4

2 回答 2

0

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

主要的.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);
        }
        });
    }
}
于 2012-08-06T01:56:41.067 回答
0

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

于 2012-08-06T02:04:55.717 回答