简体   繁体   English

Android:将按钮与用户无法看到的值相关联

[英]Android: associate a button with a value, that the user cannot see

I want to have a value (eg integer 17) associated with a button.我想要一个与按钮关联的值(例如整数 17)。 When the button is clicked I want to retrieve that value.单击按钮时,我想检索该值。 How do I do that?我怎么做?

I am readingthis since button extends TextView .我正在阅读此内容,因为按钮扩展了TextView I've scanned the XML attributes but I cannot find anything.我已经扫描了XML属性,但找不到任何内容。 I was thinking about something like casting the View that I get in my activity method to a (Button) and then call something like我正在考虑将我在活动方法中获得的视图转换为(Button) ,然后调用类似的东西

int my_database_int = button.getValue() 

I find it hard to believe that there is no such functionality?我很难相信没有这样的功能?

There is a property for Button objects in which you can store almost anything, Button对象有一个属性,您可以在其中存储几乎所有内容,
and this is the tag:这是标签:

int myvalue = 10;
button.setTag(myValue);

and then get it like:然后得到它:

int my_database_int = Integer.parseInt(button.getTag().toString());

You can also define the tag's value in xml:您还可以在 xml 中定义标签的值:

android:tag="10"

Try using the android:tag="value" attribute in your XML and use button.getTag() in your java class.尝试在您的 XML 中使用android:tag="value"属性并在您的 java 类中使用button.getTag() The method returns a object you have to typecast to your desired format.该方法返回一个对象,您必须将其类型转换为所需的格式。

Try button.getId() in Java file.在 Java 文件中尝试 button.getId()。 It will return integer unique id of button view它将返回按钮视图的整数唯一 ID

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

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