2012-03-24 5 views
8

Sto gonfiando un pulsante xml con un pulsante, più volte e sono in grado di farlo perfettamente ma il problema è quando faccio clic sul pulsante, voglio mostrare quale pulsante è stato cliccato.Quando si gonfia dinamicamente un layout xml più volte, come posso differenziare o identificare i widget Button?

public class InflateExActivity extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 

    Button b; 


    LinearLayout lLayout; 
    LayoutInflater inflater; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     for (int i = 0; i < 3; i++) { 

      inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      b = (Button) inflater.inflate(R.layout.buttons, null); 
      t = (TextView) inflater.inflate(R.layout.texts, null); 


      b.setTag(i); // you'll get 0,1,2 as 

      lLayout = (LinearLayout) findViewById(R.id.layout1); 
      lLayout.addView(b); 

      b.setOnClickListener(this); 

     } 

    } 

    public void onClick(View v) { 

     } 

} 
+0

Sapete che i nomi delle variabili possono e devono essere lunghi più di una lettera. – stimms

risposta

4

elementi che si aggiungono a livello di codice, è necessario disporre di assegnare ids a loro.

b.setId(1); 

Modificato:

public class DynamicLayoutActivity extends Activity implements OnClickListener{ 
private static final int MY_BUTTON = 9000; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LinearLayout ll = (LinearLayout)findViewById(R.id.layout1); 

    // add button 
    Button b = new Button(this); 
    b.setText("Button added dynamically!"); 
    b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    b.setId(MY_BUTTON); 
    b.setOnClickListener(this); 
    ll.addView(b); 
} 
public void onClick(View v) { 
     Toast toast; 
     Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId()); 
     switch (v.getId()) { 
     case MY_BUTTON: 
      toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG); 
      toast.setGravity(Gravity.TOP, 25, 400); 
      toast.show();  
     } 
    } 

ULTIME:

public class InflateExActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     LinearLayout lLayout; 
     Button b = null; 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     for(int i=0;i<3;i++){ 
      final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      b = (Button) inflater.inflate(R.layout.buttons, null); 
      b.setId(i); 
      lLayout = (LinearLayout) findViewById(R.id.layout1); 
      lLayout.addView(b); 
      b.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       Toast.makeText(InflateExActivity.this, "Button Clicked :"+v.getId(), 
         Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 
} 
+0

puoi dire come usarlo nel codice ?? – Rookie

+0

riferimento questo http://as400samplecode.blogspot.in/2011/10/android-programmatically-generate.html –

+0

vedi la mia risposta a cura –

3

Utilizzare il tag vista

View.setTag (tag Object);

È possibile impostare una stringa o un oggetto complesso come una classe al tag.

+0

puoi dire come usarlo nel codice ?? – Rookie

6

È possibile utilizzare setTag() su ciascun pulsante. All'interno del ciclo for puoi assegnare button.setTag(). E puoi usare getTag() per recuperare il tag del pulsante. Dopo si gonfiano il layout, aggiungere un tag al pulsante

EDIT2: Si dovrebbe gonfiare il layout e poi cercare per il vostro pulsante ID. Vedi sotto:

public class InflateExActivity extends Activity { 
      /** Called when the activity is first created. */ 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       LinearLayout lLayout; 
       final Button b = null; 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
       for(int i=0;i<3;i++){ 
        final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = inflater.inflate(R.layout.buttons, null); 
b = v.findViewById(R.id.your_button_id); 
    //     b = (Button) inflater.inflate(R.layout.buttons, null); 
         b.setTag(i); // you'll get 0,1,2 as tags 
        lLayout = (LinearLayout) findViewById(R.id.layout1); 
        lLayout.addView(b); 
        b.setOnClickListener(new OnClickListener() { 

         public void onClick(View v) { 
    int specificButton = (Integer)v.getTag();//Changed here....... 
          Toast.makeText(InflateExActivity.this, "Button Clicked"+Integer.toString(specificButton), 
            Toast.LENGTH_LONG).show(); 
         } 
        }); 
       } 
      } 
     } 
+0

Puoi modificare il codice e post..coz non sono in grado di farlo. – Rookie

+0

mostra sempre il pulsante 2 cliccato .. – Rookie

+0

ancora sta mostrando il tasto 2 cliccato ... ultimo pulsante. – Rookie

Problemi correlati