2014-03-12 58 views
-2

我正在一个大的用户界面上显示用户表单的详细信息。我希望如果用户点击包含电话号码TextViewLinearLayout。我想拨打电话intent。我已将onClick()属性添加到所有LinearLayout s。但只有第一个工作,而其他人没有任何解释将不胜感激。我的第一个LinearLayout onclick工作,而其他人不工作。为什么?

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

LinearLayout ID为LL1作品onClick()事件,而[112和休息等不起作用。我不知道为什么会发生这种情况。

+3

发布您的代码。 –

+1

发布您的代码.. – Shriram

回答

0

确保为您做以下每个布局:

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

这可能是更容易简单地声明在XML的onClick财产 - 这将避免你叫setOnClickListener每个视图。这是可以做到像这样:

<LinearLayout 
    ... 
    android:onClick="doSomething" > 

然后,在Java代码中,您将需要一个名为doSomething的公共方法接受与View类型的参数:

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

注意,你不必如果您不需要/需要,可以使用view参数进行任何操作。

+0

感谢您的帮助 – donison24x7

相关问题