Android ImageView 如何链接到网页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3536165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:39:57  来源:igfitidea点击:

How can ImageView link to web page?

android

提问by Dean-O

is it possible to make an imageview link to a web page such that when user taps on the image, it takes them to a web page?

是否可以制作指向网页的 imageview 链接,以便当用户点击图像时,它会将他们带到网页?

回答by Cristian

Just add a click listener to the image to open an URL:

只需向图像添加一个点击监听器即可打开一个 URL:

ImageView img = (ImageView)findViewById(R.id.foo_bar);
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://casidiablo.net"));
        startActivity(intent);
    }
});

回答by Konstantin Burov

That's truly possible, at onClick handler you need to start an activity with intent specifying the uri. See How to open default browserfor example.

这确实是可能的,在 onClick 处理程序中,您需要启动一个意图指定 uri 的活动。例如,请参见如何打开默认浏览器

回答by saika

another method we can even use is

我们甚至可以使用的另一种方法是

    image.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent redirect2=new Intent(getApplicationContext(),RedirectAct.class);
startActivity(redirect2);

    }
});