2013-07-04 52 views
5

如何为布局定义ID?Android将ID添加到布局

我想要的ID添加到的LinearLayout并设置一个onclick听众:

XML:

<LinearLayout 
    android:id="@+id/?????????" 
    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.????? : 
     } 
    } 
} 
+0

我认为ID是不是你的问题就在这里,你有tu把布局'android:clickable = true',因为你已经是addi了这个ID,不是吗? – zozelfelfo

回答

7

既然你有这个

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

你可以做如下

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

    } 

编辑:

如果你有按钮,然后你可以做下面。

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

然后在你的活动的活动

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

我想添加一个onClickListener到布局,而不是一个按钮。 –

+0

@ZbarceaChristian抱歉错了。 – Raghunandan

+0

@ZbarceaChristian检查编辑也。 – Raghunandan

2

最简单的方法是只需添加它在你的布局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> 

然后可以通过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

在的onCreate():

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

,有你的活动实施View.OnClickListener。

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

而不是

switch(v) 

使用

switch(v.getId()) 

,并从XML设置你的ID

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

好,我的朋友。尽管对于那些知道原因的人来说相当明显,但也许你可以在其中做些简单的解释。 – codeMagic