图像按钮属性检查

发布于 2024-12-12 17:32:43 字数 1044 浏览 5 评论 0原文

我有一个 ImageButton,单击它会显示一个对话框,用户可以在其中从相机拍照或从图库中选择。从任一来源选择图像时,我将该 ImageButton 的位图设置为像这样选择的图像

SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
DisplayPhoto.setImageBitmap(SelectedPhoto);

现在,当有人已经选择了图像并再次单击该图像时,我想显示一个不同的对话框,其中包含第三个选项“删除照片”。

我应该检查图像按钮的哪些属性以及哪些属性?

XML 中的 ImageButton

<ImageButton
                android:id="@+id/DisplayPhoto"
                android:layout_width="95dip"
                android:layout_height="95dip"
                android:layout_marginRight="8dip"
                android:background="@drawable/signup_photo_selector" android:scaleType="centerCrop" />

ImageButton 背景 XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/signup_form_photo_selected" android:state_pressed="true"/>
    <item android:drawable="@drawable/signup_form_photo"/>
</selector>

I have an ImageButton which on click i show a dialog box where users can either take a photo from the camera or choose from the gallery. On selecting image from either sources i setBitmap for that ImageButton to the image selected like this

SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
DisplayPhoto.setImageBitmap(SelectedPhoto);

Now when some one has already selected an image and click the image again i want to show a different dialog which contains a third option "Remove Photo".

What property of the image button should i check and against what ?

ImageButton in XML

<ImageButton
                android:id="@+id/DisplayPhoto"
                android:layout_width="95dip"
                android:layout_height="95dip"
                android:layout_marginRight="8dip"
                android:background="@drawable/signup_photo_selector" android:scaleType="centerCrop" />

ImageButton Background XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/signup_form_photo_selected" android:state_pressed="true"/>
    <item android:drawable="@drawable/signup_form_photo"/>
</selector>

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

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

发布评论

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

评论(1

仙气飘飘 2024-12-19 17:32:43

如果没有为图像按钮分配任何可绘制对象,imgButton.getDrawable() 会返回 null 吗?

如果没有,或者您不想获取整个可绘制对象只是为了查看它是否存在,则可以使用标签。 imgButton.setTag(object) 允许您存储图像按钮中的任何对象...每次设置其背景时,您可以标记一个值来标识其背景是否已设置。如果有用的话,您甚至可以使用不同的值来区分是使用相机设置背景还是从图库设置背景。当您想查看图像按钮是否有背景时,请使用 imgButton.getTag() 检索该对象。

编辑。以下是如何使用 setTag 和 getTag。我将使用 Integer 对象作为 ImageButton 的标记,其中值 0 表示未设置背景,值 1 表示已设置背景。如果您想让代码更清晰一些,您可以使用枚举或最终变量,但使用整数可以作为示例。

public class MainActivity extends Activity, implements OnClickListener {
  private ImageButton imgButton;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imgButton = (ImageButton)findViewById(R.id.imgID);
    imgButton.setTag(new Integer(0)); // no background
    ...
  }

  public void onClick(View view) {
    ImageButton ib = (ImageButton)view;
    int hasBackground = ib.getTag().intValue();

    if(hasBackground==0) {
      // imagebutton does not have a background. do not include remove option
      ...
    } else {
      // imagebutton has a background. include remove option
    }
  }
}

Would imgButton.getDrawable() work, since it returns null if no drawable has been assigned to the imagebutton?

If not, or if you don't want to get the entire drawable just to see if it's there, you can use a tag. imgButton.setTag(object) lets you store any object within the imagebutton... every time you set its background, you can tag a value that identifies whether its background was set. You could even use different values to differentiate whether you set its background using a camera or from the gallery, if that's useful. When you want to see if the imagebutton has a background or not, use imgButton.getTag() to retrieve the object.

Edit. Here is how you would use setTag and getTag. I will use an Integer object as the ImageButton's tag, where a value of 0 indicates no background has been set and a value of 1 indicates a background has been set. You can use an enum or final variables if you want to make the code a bit clearer, but using an Integer will work as an example.

public class MainActivity extends Activity, implements OnClickListener {
  private ImageButton imgButton;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imgButton = (ImageButton)findViewById(R.id.imgID);
    imgButton.setTag(new Integer(0)); // no background
    ...
  }

  public void onClick(View view) {
    ImageButton ib = (ImageButton)view;
    int hasBackground = ib.getTag().intValue();

    if(hasBackground==0) {
      // imagebutton does not have a background. do not include remove option
      ...
    } else {
      // imagebutton has a background. include remove option
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文