我想为一个链接到weburl的imageview onclick添加一个意图。下面你可以看到家庭片段页面的xml文件和家庭片段的java文件。请帮我解决这个问题
HomeFragment.xml
<ImageView
android:id="@+id/imageView2"
android:layout_width="80dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView1"
android:layout_marginRight="15dp"
android:src="@drawable/scial" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="130dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:src="@drawable/log" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:src="@drawable/line" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="180dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView3"
android:src="@drawable/banner" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="400dp"
android:layout_height="300dp"
android:layout_below="@+id/imageView4"
android:layout_centerHorizontal="true"
android:src="@drawable/mainn" />
</RelativeLayout>
HomeFragment.java
package com.tabs;
import com.tabs.R;
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
答案 0 :(得分:0)
使用所需的网页地址向其中一个imageview添加意图。试试这个:
image.setOnClickListener(new View.onClickListener(){
String url = "http://www.google.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(i);
});
答案 1 :(得分:0)
您是否尝试过添加onCreateView?
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://www.your_weburl.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(i);
}
});
答案 2 :(得分:0)
你应该使用图像按钮;即:ib_playPause XML
ImageButton
android:id="@+id/ib_play"
android:src="@drawable/ic_play"
JAVA: 私人ImageButton播放;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
play = (ImageButton) rootView .findViewById(R.id.ib_play);
return rootView;
}
// for onclick
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri
.parse("https://play.google.com/store");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
}