2015-10-09 38 views
-1

我想在我的應用程序中,當有人點擊圖像時,它會打開默認瀏覽器。如何將URL添加到android studio中的圖像?

我到處搜索,但沒有方法可以幫助我。

<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’(?) 和 」無法解析符號‘網站鏈接’ 和 「無法解析方法startActivity(android.content.intent)

我的問題是不同的,這個問題的答案是不工作

+2

複製:http://stackoverflow.com/questions/8356655/imageview-autolink – yongsunCN

回答

2

最快的方式設置這很可能是添加

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); 
} 
+0

嗨艾希莉,\t 我在哪裏粘貼? –

+0

頂部應該放入你的圖像xml視圖。另一部分你進入到你將要使用onclick效果的類中。可能是一個活動類或片段。 –

1

可以實現onClickimageView,並可以從那裏打開網址:

例如:

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); 
      } 
     }); 
+0

我需要粘貼嗎? –

+0

在設置內容後開啓您的活動。 –

相關問題