首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android应用程序无法打开web链接

Android应用程序无法打开web链接
EN

Stack Overflow用户
提问于 2014-02-16 21:21:12
回答 1查看 318关注 0票数 2

我有一个按钮在这个应用程序的末尾,把用户带到他们的默认浏览器和一个预定的链接。不幸的是,那个按钮坏了。我不确定这是Java的问题还是XML的问题,但两者的代码都附加在一起。

代码语言:javascript
复制
package com.example.ldsm3;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity
import android.content.DialogInterface;
import android.content.Intent;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class Finished extends Activity {

    public void onClick(Button button1) 
    {

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("URL HERE"));
        startActivity(browserIntent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finished);

        // tv is the ID for the TextView in the XML file
        TextView tv = (TextView) findViewById(R.id.textView2); 

        // set the TextView to show the score
        tv.setText(Misc.correct + "/" + Misc.total);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.finished, menu);
        return true;
    }

}

而XML代码..。

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Finished" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="69dp"
        android:text="@string/Finished" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginBottom="19dp"
        android:text="Go to the game!" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="93dp"
        android:text="0/0" />

</RelativeLayout>

我该怎么解决这个问题?是因为XML还是Java?我没有正确地定义意图吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-16 21:28:32

您需要在您的onCreate()方法中添加以下内容:

代码语言:javascript
复制
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
        //Do your stuff here
  }
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21816809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档