Onclicklistener以编程方式创建了Imagebutton

时间:2014-08-27 12:06:51

标签: android button onclicklistener

我动态创建不同的Imagebuttons:

for (int i=0 ; i<articlesArray.size() ; i++){
   table=(TableLayout)findViewById(id.showOuvragesTableLayoutCreateDevis);
   LinearLayout layout = new LinearLayout(ShowCreateOuvrages.this);
   layout.setOrientation(LinearLayout.VERTICAL);
   BitmapDrawable drawableDelete = (BitmapDrawable)getApplicationContext().getResources().getDrawable(R.drawable.deleterows);
   Bitmap bitmapDelete = drawableDelete.getBitmap();
   bitmapDelete = Bitmap.createScaledBitmap(bitmapDelete, 16, 16, true); 
   tR = new TableRow(ShowCreateOuvrages.this);
   TextView TextViewTVA = new TextView(ShowCreateOuvrages.this);         
   ImageButton ImgButtonDelete = new ImageButton(ShowCreateOuvrages.this);   
   Articles articles = articlesArray.get(i);
   TextViewTVA.setText("Remise.toString()");
   TextViewTVA.setBackgroundResource(R.drawable.cellborder);
   TextViewTVA.setPadding(5, 5, 5, 5);
   ImgButtonDelete.setBackgroundColor(80000000);
   ImgButtonDelete.setImageBitmap(bitmapDelete);        
   ImgButtonDelete.setOnClickListener(mListener);
   tR.addView(ImgButtonDelete);
   tR.addView(TextViewTVA);
   table.addView(tR);
}

和Class Listener一样:

OnClickListener mListener = new OnClickListener() {

     public void onClick(View v) {
         View myView = findViewById(R.id.showOuvragesTableLayoutCreateDevis);
            ViewGroup parent = (ViewGroup) myView.getParent();
            parent.removeView(myView);
     }
};

但它不起作用。似乎onClickListener只在一个ImageButtons上工作。

如何让我的点击听众工作?

2 个答案:

答案 0 :(得分:0)

ImgButtonDelete.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                       Toast.makeText(getApplicationContext(), 
                                "Clicked Button Index :" Click me", 
                                Toast.LENGTH_LONG).show();

                    }
                }); 

答案 1 :(得分:0)

table=(TableLayout)findViewById(R.id.showOuvragesTableLayoutCreateDevis);
           LinearLayout layout = new LinearLayout(this);
           layout.setOrientation(LinearLayout.VERTICAL);
           BitmapDrawable drawableDelete = (BitmapDrawable)getApplicationContext().getResources().getDrawable(R.drawable.deleterows);
           Bitmap bitmapDelete = drawableDelete.getBitmap();
           bitmapDelete = Bitmap.createScaledBitmap(bitmapDelete, 16, 16, true); 
           tR = new TableRow(this);
           TextView TextViewTVA = new TextView(this);         
           ImageButton ImgButtonDelete = new ImageButton(this);   

           TextViewTVA.setText("Remise.toString()");
           TextViewTVA.setBackgroundResource(R.drawable.cellborder);
           TextViewTVA.setPadding(5, 5, 5, 5);
           ImgButtonDelete.setBackgroundColor(80000000);
           ImgButtonDelete.setImageBitmap(bitmapDelete);  
           tR.addView(ImgButtonDelete);
           tR.addView(TextViewTVA);
           table.addView(tR);
           ImgButtonDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ViewGroup parent = (ViewGroup) table.getParent();
                 parent.removeView(table);
            }
        });