如何在android studio中为图像添加URL?

时间:2015-10-09 15:41:42

标签: java android xml

我希望在我的应用中,当有人点击使用默认浏览器打开的图片时。

我到处搜索,但没有任何方法可以帮助我。

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Home_Activity"
android:background="#FF0000">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/AppTitleimage1"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@mipmap/fra"
    android:contentDescription="@string/AppTitleImageS"
    android:autoLink="web"
    android:clickable="true" />

如何添加指向在默认浏览器中打开的图像的链接

由于

所以我发现了这个:

ImageView imgLink=(ImageView)findViewById(R.id.weblink);
    imgLink.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent link=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
            startActivity(link);    
        }
    });

但是当我将它粘贴到我的java类中时,它说:

“无法解析方法'findViewById'(?) 和 “无法解析符号'weblink' 和 “无法解析方法startActivity(android.content.intent)

我的问题不同,答案无效

3 个答案:

答案 0 :(得分:5)

最快的设置方法可能是添加

android:onClick="goToUrl"

到您的imageview并将此功能添加到您的班级。

 private void goToUrl (View view) {
        String url = "http://www.google.com"
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
 }

答案 1 :(得分:1)

您可以为onClick实施imageView,并可以从那里打开网址:

示例:

ImageView imageView = (ImageView) findViewById(R.id.AppTitleimage1);
imageView.setOnClickListener(new View.OnClickListener() {  
        public void onClick(View v)
            {
                Uri uri = Uri.parse("http://www.google.com"); 
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent); 
            }
         });

答案 2 :(得分:0)

try this code i think is working:

ImageView imgLink=(ImageView)findViewById(R.id.AppTitleimage1);
imgLink.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        String url = "http://www.example.com";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);   
    }
});