Вам понадобится только один onClick
событие для обработки всех ImageView
кликов.
Используйте атрибут android:tag
, чтобы назначить URL-адрес, который вы хотите открыть.
Используйте атрибут , чтобы назначить метод, который обрабатывает событие click.
В XML:
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/yourimage1"
android:tag="http://site_1.com"
android:onClick="openBrowser"/>
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/yourimage2"
android:tag="http://site_2.com"
android:onClick="openBrowser"/>
В деятельности:
public void openBrowser(View view){
//Get url from tag
String url = (String)view.getTag();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
//pass the url to intent data
intent.setData(Uri.parse(url));
startActivity(intent);
}
Хорошо, так, что мне нужно изменить все мои Imageviews к ImageButton и добавить Intent к каждому методу OnClick. Спасибо, я попробую это :) – dzim
@dzim Вам не нужно менять ImageViews в ImageButton, и вам нужен только один метод onClick. См. Мой ответ. – ramaral