- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 ImageButton,单击它会显示一个对话框,用户可以在其中从相机拍摄照片或从图库中选择。从任一来源选择图像时,我将该 ImageButton 的位图设置为这样选择的图像
SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
DisplayPhoto.setImageBitmap(SelectedPhoto);
现在,当有人已经选择了一张图片并再次单击该图片时,我想显示一个不同的对话框,其中包含第三个选项“删除照片”。
我应该检查图像按钮的什么属性?
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 背景 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>
最佳答案
imgButton.getDrawable() 会工作吗,因为如果没有为 imagebutton 分配可绘制对象,它会返回 null?
如果没有,或者如果您不想获取整个可绘制对象只是为了查看它是否存在,您可以使用标签。 imgButton.setTag(object) 允许您在图像按钮中存储任何对象...每次设置其背景时,您都可以标记一个值来标识其背景是否已设置。如果有用的话,您甚至可以使用不同的值来区分您是使用相机还是从画廊设置背景。当您想查看图像按钮是否有背景时,请使用 imgButton.getTag() 来检索对象。
编辑。以下是您将如何使用 setTag 和 getTag。我将使用一个 Integer 对象作为 ImageButton 的标签,其中 0 值表示未设置背景,1 值表示已设置背景。如果您想让代码更清晰一些,您可以使用枚举或最终变量,但使用 Integer 将作为示例。
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
}
}
}
关于android - ImageButton 属性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7945886/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!