Android 我的第一个线性布局onclick可以工作,而其他的不行。为什么?

Android 我的第一个线性布局onclick可以工作,而其他的不行。为什么?,android,xml-layout,Android,Xml Layout,我正在开发一个大UI来显示用户表单的细节。如果用户单击包含电话号码的LinearLayout,我希望看到它。我想打个电话intent。我已将onClick()属性添加到所有LinearLayouts。但只有第一种方法有效,而其他方法无效。我们将感谢任何解释 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_conten

我正在开发一个大UI来显示用户表单的细节。如果用户单击包含电话号码的
LinearLayout
,我希望看到它。我想打个电话
intent
。我已将
onClick()
属性添加到所有
LinearLayout
s。但只有第一种方法有效,而其他方法无效。我们将感谢任何解释

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp" >

                <ImageButton
                    android:id="@+id/imageButtonMSG1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#f8f3c2"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="20dp"
                    android:src="@drawable/msg" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:id="@+id/ll1"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:layout_alignParentLeft="true"
                    android:layout_toLeftOf="@+id/imageButtonMSGMobile"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/textViewMobile1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:text="Small Text"
                        android:textColor="#790202"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                    <TextView
                        android:id="@+id/textViewCperson1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="20dp"
                        android:text="Cperson1"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#E14343" />

                </LinearLayout>

                <TextView
                    android:id="@+id/textView201"
                    android:layout_width="0.1dp"
                    android:layout_height="40dp"
                    android:background="#8E6364"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="60dp"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </RelativeLayout>
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="2dp" >

                <TextView
                    android:id="@+id/textView51"
                    android:layout_width="fill_parent"
                    android:layout_height="0.2dp"
                    android:background="#8E6364"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/ll2"
                    android:layout_alignParentLeft="true"
                    android:clickable="true"
                    android:layout_toLeftOf="@+id/imageButtonMSGHome"
                    android:orientation="vertical" >

                <TextView
                    android:id="@+id/textViewMobile2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:text="Small Text"
                    android:duplicateParentState="true"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#790202" />

                <TextView
                    android:id="@+id/textViewCperson2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="20dp"
                    android:text="Cperson2"
                    android:duplicateParentState="true"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#E14343" />

                </LinearLayout>
                 <TextView
                    android:id="@+id/textView202"
                    android:layout_width="0.2dp"
                    android:layout_height="40dp"
                    android:background="#8E6364"
                     android:layout_alignParentRight="true"
                    android:layout_marginRight="60dp"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <ImageButton
                    android:id="@+id/imageButtonMSG2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#f8f3c2"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="20dp"
                    android:src="@drawable/msg" />
            </RelativeLayout>


id为ll1的
LinearLayout
可用于
onClick()
事件,而ll2和其他其余部分则不起作用。我不知道为什么会发生这种情况。

确保对每个布局执行以下操作:

LinearLayout ll1 = (LinearLayout )findViewById(R.id.ll1);
ll1.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View v) {
        //Do stuff here     
    }
   });

在XML中简单地声明
onClick
属性可能更容易-这将避免为每个视图调用
setOnClickListener
。可以这样做:

<LinearLayout
    ...
    android:onClick="doSomething" >
注意,如果不想/不需要,则不必对
视图
参数执行任何操作

public void doSomething(View view) {
// React to click here
}