简体   繁体   English

使用longclick更改imagebutton图像(从图库)

[英]Changing imagebutton image(from gallery) with longclick

I've searched the forums, but not have found any specific or understandable answers for my problem. 我已经搜索了论坛,但是没有找到针对我的问题的任何具体或可以理解的答案。

I'd like to change my Imagebutton image to a picture, selected from the gallery. 我想将我的Imagebutton图片更改为从图库中选择的图片。 Prefferrably the image should stay changed after closing the application. 关闭应用程序后,图像应该保持不变。

My XML for the button is here: 我在按钮上的XML在这里:

<ImageButton
        android:id="@+id/eat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:adjustViewBounds="true"
        android:background="@drawable/eat"
        android:clickable="true"
        android:longClickable="true"
        android:scaleType="fitCenter" />

The java code for playing the sound is here with the OnClick method. 此处用于播放声音的Java代码与OnClick方法一起使用。

    ImageButton eat = (ImageButton) findViewById (R.id.eat);
    eat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp1.start();
        }
    });

I would like to add the OnLongClick method here too, (since to OnClick is allready taken and the image replacing should be a little different), but havent found the right way. 我也想在这里添加OnLongClick方法(因为已经准备好使用OnClick并且图像替换应该有所不同),但是还没有找到正确的方法。 Can You please guide me a little bit? 你能指导我一点吗?

You need to return true from image's onLongClickListener. 您需要从图片的onLongClickListener返回true。

Like this: 像这样:

eat.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        //do something
        return true;
    }

});

eat.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mp1.start();
    }
});

This will not cause the image's onClickListener to be called as it means that the action has already been handled in longClickListener. 这不会导致图像的onClickListener被调用,因为这意味着该操作已在longClickListener中处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM