简体   繁体   English

在Android中反转/突出显示按钮的适当方法

[英]Appropriate way to invert/highlight a button in android

In my app I dynamically create a list of buttons. 在我的应用程序中,我动态创建了一个按钮列表。 The user should be able to click on them. 用户应该能够单击它们。 A short click would select the buttom as the current item, while a long click should enter the editor for this item. 短按将选择该按钮作为当前项目,长按应进入该项目的编辑器。

Now I wonder how I can determine and set the colors that i should use. 现在,我想知道如何确定和设置应该使用的颜色。 My first idea was to simply read the background and textcolor and switch them, however I'm not sure if this would be really the appropriate way to do this. 我的第一个想法是简单地读取背景和文本颜色并进行切换,但是我不确定这是否真的是合适的方法。 So I was wondering if there is a an appropriate way of how to retrieve colors. 所以我想知道是否有适当的方法来检索颜色。

Of course I could hardcode some colors, but I don't know what color scheme the user has set and they might not be visible in a good way. 当然,我可以对一些颜色进行硬编码,但是我不知道用户设置了哪种颜色方案,因此可能无法很好地看到它们。

Please note, that, since I have to create the buttons dynamically, I can not set it in the XML. 请注意,由于必须动态创建按钮,因此无法在XML中进行设置。

You could simply use a ToggleButton , so android will take care of marking a "clicked" button as selected. 您可以只使用ToggleButton ,因此android将负责将“ clicked”按钮标记为选中状态。

Since ToggleButton is a View , it has a setOnLongClickListener(Listener) -method , which can be used to make the ToggleButton long-clickable. 由于ToggleButton是View ,因此它具有setOnLongClickListener(Listener) -method ,可用于使ToggleButton长按。

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

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