我希望我的imageView
能够在用户点击时打开url/link
。我搜索了一些例子但主要是它不适合片段。任何人都可以帮助我吗?
HomeFragment.java
public class HomeFragment extends Fragment{
{
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageView img = (ImageView)rootView.findViewById(R.id.imageViewTwitter);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://twitter.com"));
startActivity(intent);
}
});
ImageView img2 = (ImageView)rootView.findViewById(R.id.imageViewIG);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://www.instagram.com/?hl=en"));
startActivity(intent);
}
});
ImageView img3 = (ImageView)rootView.findViewById(R.id.imageViewFB);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://www.facebook.com"));
startActivity(intent);
}
});
return rootView;
}}
fragmnet_main.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:background="@drawable/back"
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="com.example.eventstory.MainActivity$PlaceholderFragment" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/homepage" />
<ImageView
android:id="@+id/imageViewIG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:src="@drawable/instagramlogo"
/>
<ImageView
android:id="@+id/imageViewTwitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageViewIG"
android:src="@drawable/twitter" />
<ImageView
android:id="@+id/imageViewFB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView2"
android:layout_alignTop="@+id/imageViewIG"
android:src="@drawable/logofacebook" />
唯一的ImageView
工作是imageViewTwitter
,但它链接到facebook.com而不是twiiter。另一个ImageView
无效
答案 0 :(得分:1)
必须足够:
...
...
ImageView img = (ImageView)rootView.findViewById(R.id.imageViewTwitter);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com"));
getActivity().startActivity(intent);
}
});
ImageView img2 = (ImageView)rootView.findViewById(R.id.imageViewIG);
img2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.instagram.com/?hl=en"));
getActivity().startActivity(intent);
}
});
ImageView img3 = (ImageView)rootView.findViewById(R.id.imageViewFB);
img3.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.gmail.com"));
getActivity().startActivity(intent);
intent.setData(Uri.parse("https://www.facebook.com"));
startActivity(intent);
}
});
...
...
您的问题是您要将onClickListeners
分配给同一个ImageView(img)!
ImageView img2 = (ImageView)rootView.findViewById(R.id.imageViewIG);
img.setOnClickListener...
ImageView img3 = (ImageView)rootView.findViewById(R.id.imageViewIG);
img.setOnClickListener...
答案 1 :(得分:0)
以下代码可用于在浏览器中打开 link / url 。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.gmail.com"));
getActivity().startActivity(intent);
背后的原因startActivity()
不在片段中工作,因为它是为Activity
而不是片段写的。为了能够调用此函数,您需要获取此应用程序的上下文
示例:
getApplictionContext().startActivity("call your relevant intent");