将内容描述设置为图像按钮时出现问题

Posted

技术标签:

【中文标题】将内容描述设置为图像按钮时出现问题【英文标题】:Problems setting the content description to image button 【发布时间】:2012-07-14 17:25:45 【问题描述】:

您好,我正在尝试为我的按钮购买设置内容描述,当我尝试访问它时,返回给我的值为 null。

这是按钮的代码。

//This is the button of the payment.
ImageButton make_pay = new ImageButton(this);
make_pay.setBackgroundResource(R.drawable.add_product);
makePay.addView(make_pay);
makePay.setContentDescription("Precio");

这是我用来访问的代码:

make_pay.setOnClickListener(new View.OnClickListener()                                         

        @Override
        public void onClick(View makepay) 
            LinearLayout wrap_area = (LinearLayout)findViewById(R.id.division2);
            TextView test = new TextView(FrontActivity.this);
            wrap_area.addView(test);
            if (makepay.getContentDescription() == null)
                    test.setText("Precio:1");
            else
                    test.setText(makepay.getContentDescription().toString());
            
        );

【问题讨论】:

【参考方案1】:

您正在将内容描述设置为 makePay 对象(不管它是什么,可能是一个 ViewGroup)。但是,您将侦听器设置为 make_pay ImageButton,这是侦听器参数接收到的那个。因此,它的内容描述不是分配给另一个对象的描述。

尝试改变这个:

makePay.setContentDescription("Precio");

用这个:

make_pay.setContentDescription("Precio");

无论如何,尽量不要以类似的方式命名您的对象。这可能会导致很大的混乱。

【讨论】:

以上是关于将内容描述设置为图像按钮时出现问题的主要内容,如果未能解决你的问题,请参考以下文章