我有一个LinearLayout
包含两个RelativeLayouts
.
这是 XML 代码:
<LinearLayout
android:id="@+id/SaleSwithcBarView_navigation_bar_LinrearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:id="@+id/SaleSwithcBarView_users_created_bar_button_RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/SaleSwithcBarView_users_favorite_bar_button_RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
</LinearLayout>
现在我试图检测clickListner
哪个视图(从这两个RelativeLayouts
被点击),我试图这样做(java代码):
navigationBar.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View navigationButton)
{
Toast.makeText(context, "hope here: " + navigationButton.getId(), Toast.LENGTH_LONG).show();
switch (navigationButton.getId())
{
case USER_CREATED_BUTTON_ID:
Toast.makeText(context, "here", Toast.LENGTH_LONG).show();
switchTodDisplay(USER_CREATED_BUTTON_ID + BINDER_OFFSET);
break;
case USER_FAVORITE_BUTTON_ID:
Toast.makeText(context, "or here", Toast.LENGTH_LONG).show();
switchTodDisplay(USER_CREATED_BUTTON_ID + BINDER_OFFSET);
break;
default:
break;
}
}
});
the navigationBar
is the LinearLayout
and the USER_CREATED_BUTTON_ID
(it is final int
) 是左边的 idRelativeLayout
并且USER_FAVORITE_BUTTON_ID
是右边的 id RelativeLayout
。我以编程方式分配了两个 id,如下所示:
userCreatedSalesBarButton.setId(USER_CREATED_BUTTON_ID);
userFavoriteSalesBarButton.setId(USER_FAVORITE_BUTTON_ID);
那么我怎样才能检测到哪些LinearLayout
子视图被点击了。我不想clickListeriner
为每个RelativeLayout
.
谢谢