2012-07-15 62 views
2

您好,我試圖設置內容描述到我的按鈕購買時,當我嘗試訪問它的返回給我的值爲空。將內容描述設置爲圖像按鈕的問題

這裏是按鈕的代碼。

//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()); 
      } 
     }); 
} 

回答

4

您設置的內容描述makePay對象(不管它是什麼,probabbly一個ViewGroup中)。但是,接下來,您將偵聽器設置爲make_pay ImageButton,它是偵聽器參數接收的偵聽器。因此,它的內容描述不是分配給其他對象的內容。

嘗試修改此:

makePay.setContentDescription("Precio"); 

與此:

make_pay.setContentDescription("Precio"); 

反正儘量不要來命名這種類似的方式你的對象。這可能會導致很大的困惑。