简体   繁体   English

如何获得按钮的 drawableTop 参考?

[英]How can I have reference for drawableTop of a Button?

I declared a drawable inside my Button and I want to get a reference for that because I created a method where I display a Dialog, extract the text and drawable of that button and use it to change the text and icon of a dialog dynamically.我在我的 Button 中声明了一个可绘制对象,我想获得一个参考,因为我创建了一个显示对话框的方法,提取该按钮的文本和可绘制对象,并使用它来动态更改对话框的文本和图标。

Please do check this snippet:请检查此代码段:

private fun showProductDialog(button : Button){
        //extract product info selected from button

        //show dialog
        val dialog = Dialog(this)
        dialog.setContentView(R.layout.dialog_product)
        //set text and icon
        val titleLayout = dialog.findViewById<LinearLayout>(R.id.ll_product_label)
        val titleText = titleLayout.findViewById<HelveticaBoldTextView>(R.id.tv_category_click)
        val titleIcon = titleLayout.findViewById<ImageView>(R.id.iv_product_icon)

        //get text of button, set to titleText
        titleText.text = button.text.toString().uppercase()

        //extract drawableTop of Button then
        //set image for titleIcon = drawable in Button
        
        dialog.show()
    }

To get DrawableTop you can use this要获得 DrawableTop 你可以使用这个

 Drawable[] drawables = button.getCompoundDrawables();

 Drawable d = drawables[1];

To set DrawableTop you can use this要设置 DrawableTop 你可以使用这个

 button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon, 0, 0);

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

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