2013-07-04 11 views
5

Come si definisce un ID per un layout?Android aggiungi ID a un layout

Sto cercando di aggiungere un ID per un LinearLayout e l'impostazione di un ascoltatore onclick:

XML:

<LinearLayout 
    android:id="@+id/?????????" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:onClick="btnHandler" > 
</LinearLayout> 

Classe:

public class MainActivity extends Activity { 
    //.... 

    public void btnHandler(View v){ 
     switch(v) 
     { 
      case R.id.????? : 
     } 
    } 
} 
+0

Penso che l'ID non è il tuo problema qui, avete tu metti il ​​layout 'android: clickable = true' perché sei già addi ng l'id, non è vero? – zozelfelfo

risposta

7

Dal momento che si dispone di questo

<LinearLayout 
android:id="@+id/linearlayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:onClick="btnHandler" > 
</LinearLayout> 

si può fare come di seguito

public void btnHandler(View v) 
    { 
     switch(v.getId()) // use v.getId() 
    { 
     case R.id.linearlayout : 
     break; // also don't forget the break; 
    } 

    } 

Edit:

Se avete il pulsante allora si può fare come sotto.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/linearlayout" 
    android:onClick="clickEvent" 
    android:orientation="vertical" > 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/bt1" 
    android:text="button" 
    android:onClick="clickEvent" 
    /> 

</LinearLayout> 

Poi nella vostra attività

public void clickEvent(View v) 
{ 
     switch(v.getId()) 
     { 
      case R.id.linearlayout : 
       Log.i("......"," linear layout clicked"); 
      break; 
      case R.id.bt1 : 
     Log.i("......"," button clicked"); 
      break; 
     } 
} 
+0

Voglio aggiungere un onClickListener al layout e non a un pulsante. –

+0

@ZbarceaChristian mi dispiace. – Raghunandan

+0

@ZbarceaChristian controlla anche la modifica. – Raghunandan

2

Il modo più semplice sarebbe quella di Just Add nel tuo layout xml:

<LinearLayout 
    android:id="@+id/myId" <!-- the part where the id is being created --> 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:onClick="btnHandler" > 
</LinearLayout> 

Può quindi essere referenziato dal codice tramite your.package.R.id.myId.

1
<LinearLayout 
    android:id="@+id/lineaLayoutId" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:onClick="btnHandler" > 
</LinearLayout> 
public class MainActivity extends Activity { 
    //.... 

    public void btnHandler(View v){ 
     switch(v) 
     { 
      case R.id.lineaLayoutId : 
      break; 
     } 
    } 
} 
0

In onCreate() della vostra attività:

findViewById(R.id.?????????).setOnClickListener(this); 

e avere la vostra attività implementare View.OnClickListener.

@Override 
public void onClick(View view) { 
    if(view.getId() == R.id.?????????) { 
     //your code for on click 
    } 
} 
3

Invece di

switch(v) 

uso

switch(v.getId()) 

e impostare l'ID dalla xml

android:id="@+id/idValue" 
+0

Buona presa, amico mio. Anche se è abbastanza ovvio per quelli che sanno perché, forse potresti aggiungere una breve spiegazione a quello che sta facendo ... – codeMagic

Problemi correlati