您好,我正在尝试为我的按钮购买设置内容描述,当我尝试访问它时,返回给我的值为 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());
}
});
}
最佳答案
您正在将内容描述设置为 makePay 对象(无论它是什么,可能是一个 ViewGroup)。然后,您将监听器设置为 make_pay ImageButton,这是监听器参数接收的监听器。因此,它的内容描述不是分配给另一个对象的内容描述。
尝试更改此设置:
makePay.setContentDescription("Precio");
这样:
make_pay.setContentDescription("Precio");
无论如何,尽量不要以如此相似的方式命名你的对象。这可能会导致很大的困惑。
关于android - 将内容描述设置为图像按钮时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11494576/