2013-07-04 17 views
5

Jak zdefiniować identyfikator do układu?Android dodaj identyfikator do układu

Próbuję dodać identyfikator do linearlayout i ustawienie detektora onclick:

XML:

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

Klasa:

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

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

myślę, że identyfikator nie jest twój problem tutaj masz tu umieścić układ 'android: clickable = true', ponieważ już jesteś addi ng identyfikator, prawda? – zozelfelfo

Odpowiedz

7

Skoro masz ten

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

Można zrobić jak poniżej

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

    } 

Edit:

Jeśli masz przycisk wtedy można zrobić jak poniżej.

<?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> 

Następnie w swojej działalności

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

Chcę dodać onClickListener do układu, a nie do przycisku. –

+0

@ZbarceaChrześcijanin przepraszam mistook. – Raghunandan

+0

@ZbarceaChristian sprawdź także edycję. – Raghunandan

2

Najprostszym sposobem byłoby po prostu dodaj w swoim układzie 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> 

Można go następnie przywołać z kodu poprzez 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

W onCreate() swojej działalności:

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

i mają swoją aktywność realizować View.OnClickListener.

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

Zamiast

switch(v) 

użycie

switch(v.getId()) 

i ustawić swój identyfikator z xml

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

Dobry połów, przyjacielu. Chociaż jest to dość oczywiste dla tych, którzy wiedzą dlaczego, może moglibyście dodać krótkie wyjaśnienie tego, co robią ... – codeMagic

Powiązane problemy