android onclick方法想要图像路径

发布于 2025-01-01 12:48:48 字数 1909 浏览 2 评论 0原文

我在我的应用程序中显示来自网络的图像。 它运行完美

我的问题是:

当我单击图像时获取当前图像 URL 的方法、函数是什么? (我将 url 作为字符串传递以进行显示)。

我使用了这种方法:

private String getAnImageUrl() {
        i++;
        if(i >= images.length){
            i = 0;
        }

public class ImageExampleXML extends Activity {
private String reviewImageLink;

private final String images[] = {"http://clients..../britney.jpg",
                                     "http://client...../evala.jpg", 
                                     "http://clients.......ashlee.jpg",
                                     "http://clients.......mple_1.jpg",
                                                                         };
    private int i = 0;

    private String imgUrl;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final Button loadImage = (Button) findViewById(R.id.button);

        final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);

        loadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageDrawable(getAnImageUrl());
            }
        });



      image.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

             imgUrl = getAnImageUrl();
                Toast.makeText(getApplicationContext(), imgUrl+ " this one", Toast.LENGTH_LONG).show();     
            // TODO Auto-generated method stub

        }
    });  

        return images[i];
    }

我通过按钮更改图像。 当我下次点击时会出现问题,它会更改 URL 但图像保持不变。

请参阅 getAnImageUrl() 导致的 onclick 中的 image.setOnClickListener 方法;

单击时我更改了显示名称.. 请告诉我如何找到当前图像网址...

注意:我使用吐司只是为了检查我得到了什么?

提前致谢。

I display image from the web in my application.
It is running perfectly

My Question is :

what is method,function to take current image URL when I click on image.?
(I passed url as string for display).

I used this method:

private String getAnImageUrl() {
        i++;
        if(i >= images.length){
            i = 0;
        }

public class ImageExampleXML extends Activity {
private String reviewImageLink;

private final String images[] = {"http://clients..../britney.jpg",
                                     "http://client...../evala.jpg", 
                                     "http://clients.......ashlee.jpg",
                                     "http://clients.......mple_1.jpg",
                                                                         };
    private int i = 0;

    private String imgUrl;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final Button loadImage = (Button) findViewById(R.id.button);

        final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);

        loadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageDrawable(getAnImageUrl());
            }
        });



      image.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

             imgUrl = getAnImageUrl();
                Toast.makeText(getApplicationContext(), imgUrl+ " this one", Toast.LENGTH_LONG).show();     
            // TODO Auto-generated method stub

        }
    });  

        return images[i];
    }

I changed image through the button.
there is problem when I clicked next time it will change URL but image remain same.

please see the method image.setOnClickListener in that onclick due to getAnImageUrl();

i changed the display name when clicked ..
please tell me how I can find current image url...

Note: I used toast only to check what i got ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2025-01-08 12:48:48

你可以做的就是

loadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgUrl = getAnImageUrl();
                image.setImageDrawable(imgUrl);
            }
        });

      image.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), imgUrl+ " this one", Toast.LENGTH_LONG).show();     
            // TODO Auto-generated method stub

        }

或者你也可以 User setTag(Object object);查看方法,例如

loadImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = getAnImageUrl();
                    image.setImageDrawable(url);
                    image.setTag(url);
                }
            });

          image.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), image.getTag()+ " this one", Toast.LENGTH_LONG).show();     
                // TODO Auto-generated method stub

            }

如果您仍然遇到图像未更改的问题。然后请分享您的LoaderImageView.java代码作为图像请求,更新是在其中处理的。

Simply what you can do is

loadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgUrl = getAnImageUrl();
                image.setImageDrawable(imgUrl);
            }
        });

      image.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
                Toast.makeText(getApplicationContext(), imgUrl+ " this one", Toast.LENGTH_LONG).show();     
            // TODO Auto-generated method stub

        }

Or you can also User setTag(Object object); method of view e.g

loadImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = getAnImageUrl();
                    image.setImageDrawable(url);
                    image.setTag(url);
                }
            });

          image.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), image.getTag()+ " this one", Toast.LENGTH_LONG).show();     
                // TODO Auto-generated method stub

            }

And If you still have problem that image is not change. Then Please share your code of LoaderImageView.java as the image request , update is handled in that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文