我尝试过2个不同的链接但没有成功。
https://stackoverflow.com/questions/3536165/how-can-imageview-link-to-web-page
和
https://stackoverflow.com/questions/3505788/how-to-open-default-browser/3505804#3505804
如果这是一个简单的解决方案,我会道歉但是我很高兴并且感到沮丧,因为根据这些例子它应该有效。
这是我的Xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:background="@color/grey"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="5sp" >
<ImageView
android:id="@+id/logo_about"
android:layout_width="85dp"
android:layout_height="105dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.11"
android:src="@drawable/web_icon" />
</LinearLayout>
<TextView
android:id="@+id/tv_content_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:background="@color/grey"
android:gravity="center_horizontal"
android:padding="5sp"
android:text="@string/info_about_us"
android:textColor="@color/black"
android:textSize="15sp" />
<!-- Contact Us -->
<RelativeLayout
android:id="@+id/contact"
android:clickable="true"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="@drawable/btn_bg" >
<ImageView
android:id="@+id/img_icon"
android:clickable="true"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="@drawable/icon_email" />
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/img_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="@+id/img_indicator"
android:layout_toRightOf="@+id/img_icon"
android:singleLine="true"
android:text="@string/title_contact_us"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name"
android:layout_alignRight="@+id/tv_name"
android:layout_below="@+id/tv_name"
android:singleLine="true"
android:text="@string/info_contact_us"
android:textColor="@color/black"
android:textSize="13sp" />
<ImageView
android:id="@+id/img_indicator"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="@drawable/ic_action_next_item" />
<View
android:id="@+id/devider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/grey" />
</RelativeLayout>
<!-- Facebook -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:clickable="true"
android:background="@drawable/btn_bg" >
<ImageView
android:id="@+id/facebook_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="@drawable/icon_facebook" />
<TextView
android:id="@+id/tv_name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/facebook_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="@+id/facebook_icon"
android:layout_toRightOf="@+id/facebook_icon"
android:singleLine="true"
android:text="@string/title_facebook"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_content1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name1"
android:layout_alignRight="@+id/tv_name1"
android:layout_below="@+id/tv_name1"
android:singleLine="true"
android:text="@string/info_facebook"
android:textColor="@color/black"
android:textSize="13sp" />
<ImageView
android:id="@+id/img_indicator1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="@drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/grey" />
</RelativeLayout>
<!-- Google Plus -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:clickable="true"
android:background="@drawable/btn_bg" >
<ImageView
android:id="@+id/google_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="@drawable/icon_googleplus" />
<TextView
android:id="@+id/tv_name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/google_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="@+id/google_icon"
android:layout_toRightOf="@+id/google_icon"
android:singleLine="true"
android:text="@string/title_google_plus"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_content2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name2"
android:layout_alignRight="@+id/tv_name2"
android:layout_below="@+id/tv_name2"
android:singleLine="true"
android:text="@string/info_google_plus"
android:textColor="@color/black"
android:textSize="13sp" />
<ImageView
android:id="@+id/img_indicator2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="@drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/grey" />
</RelativeLayout>
<!-- Website -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="@drawable/btn_bg" >
<ImageView
android:id="@+id/website_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="@drawable/icon_website" />
<TextView
android:id="@+id/tv_name3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/website_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="@+id/img_indicator3"
android:layout_toRightOf="@+id/website_icon"
android:singleLine="true"
android:text="@string/title_website"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_content3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name3"
android:layout_alignRight="@+id/tv_name3"
android:layout_below="@+id/tv_name3"
android:singleLine="true"
android:text="@string/info_website"
android:textColor="@color/black"
android:textSize="13sp" />
<ImageView
android:id="@+id/img_indicator3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="@drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/grey" />
</RelativeLayout>
<!-- Rate Us -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="@drawable/btn_bg" >
<ImageView
android:id="@+id/rate_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="@drawable/icon_rate_me" />
<TextView
android:id="@+id/tv_name4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/rate_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="@+id/img_indicator4"
android:layout_toRightOf="@+id/rate_icon"
android:singleLine="true"
android:text="@string/title_rate_us"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_content4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name4"
android:layout_alignRight="@+id/tv_name4"
android:layout_below="@+id/tv_name4"
android:singleLine="true"
android:text="@string/info_rate_us"
android:textColor="@color/black"
android:textSize="13sp" />
<ImageView
android:id="@+id/img_indicator4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="@drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/grey" />
</RelativeLayout>
<!-- More Apps -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="@drawable/btn_bg" >
<ImageView
android:id="@+id/apps_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="@drawable/icon_more_app" />
<TextView
android:id="@+id/tv_name5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/apps_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="@+id/img_indicator5"
android:layout_toRightOf="@+id/apps_icon"
android:singleLine="true"
android:text="@string/title_more_app"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_content5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name5"
android:layout_alignRight="@+id/tv_name5"
android:layout_below="@+id/tv_name5"
android:singleLine="true"
android:text="@string/info_more_app"
android:textColor="@color/black"
android:textSize="13sp" />
<ImageView
android:id="@+id/img_indicator5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="@drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/grey" />
</RelativeLayout>
</LinearLayout>
现在我的Fragment.java
package com.wny.wecare.fragment;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.wny.wecare.R;
public class AboutUsFragment extends Fragment implements OnClickListener {
public static final String TAG = AboutUsFragment.class.getSimpleName();
private View mRootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
return mRootView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
});
}
}
答案 0 :(得分:0)
这样做而不是兄弟
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
if(v == img)
Context.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://infinitycodeservices.com"))); //context stands 4 ur activity
}
编辑1:我将intent
更改为Intent
这是早期变量错误的原因..
重试&amp; SEEE
让我知道它是否有效.. 我感觉我留下了什么。
答案 1 :(得分:0)
根据我的理解,你的public void onClick(View v)
没有叫你刚刚声明,但是没有设置监听器,那么当你在onClick()
上设置一个永远不会调用的ImageView的监听器时。
我认为您应setListener()
使用匿名内部类onCreateView()
{/ 1}}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
});
return mRootView;
}
或如果您仍然使用工具View.OnClickListener()
,请使用此
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(this);
return mRootView;
}
@Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
希望这有帮助
答案 2 :(得分:0)
经过几个小时的阅读后,我终于解决了这个问题。 感谢此链接Correct way to call onClickListener on fragments
谢谢大家的帮助,非常感谢。
这是我的完整AboutUsFragment.Java
package com.wny.wecare.fragment;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.wny.wecare.MainActivity;
import com.wny.wecare.R;
public class AboutUsFragment extends Fragment implements OnClickListener {
ImageView Contact;
ImageView Facebook;
ImageView Google;
ImageView Website;
ImageView Rate;
ImageView Apps;
public static final String TAG = AboutUsFragment.class.getSimpleName();
private View mRootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mRoot = inflater.inflate(R.layout.fragment_about_us, null);
Contact = (ImageView) mRoot.findViewById(R.id.contact_icon);
Contact.setOnClickListener(this);
Facebook = (ImageView) mRoot.findViewById(R.id.facebook_icon);
Facebook.setOnClickListener(this);
Google = (ImageView) mRoot.findViewById(R.id.google_icon);
Google.setOnClickListener(this);
Website = (ImageView) mRoot.findViewById(R.id.website_icon);
Website.setOnClickListener(this);
Rate = (ImageView) mRoot.findViewById(R.id.rate_icon);
Rate.setOnClickListener(this);
Apps = (ImageView) mRoot.findViewById(R.id.apps_icon);
Apps.setOnClickListener(this);
return mRoot;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.contact_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:infinitycodeservices@gmail.com")));
break;
case R.id.facebook_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/infinitycodeservices")));
break;
case R.id.google_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/105796163192090141980/")));
break;
case R.id.website_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
break;
case R.id.rate_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=InfinityCodeServices")));
break;
case R.id.apps_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=InfinityCodeServices")));
break;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
Intent intent;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
String cameback="CameBack";
intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("Comingback", cameback);
startActivity(intent);
return true;
}
return false;
}
}
答案 3 :(得分:-1)
你在片段中,所以我认为你应该使用:
调用startActivity()方法getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));