2017-11-11 162 views
-1

我在CardView内有TextView。我希望在点击Textview时发生一些功能,但我的没有响应点击,因为方法没有被调用。什么是问题?请帮帮我。为什么onClick不能在我的代码中工作?

这里是我的代码:

XML

<android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="15dp"> 
       <LinearLayout 

        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Application info" 
         android:textAlignment="center" 
         android:layout_gravity="left" 
         android:layout_margin="10dp"/> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/ApplicationHide" 
        android:layout_width="match_parent" 

        android:layout_height="match_parent" 
        android:layout_margin="30dp" 
        android:orientation="vertical" 
        android:weightSum="11"> 


        <android.support.design.widget.TextInputLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

        <EditText 
         android:focusable="false" 
         android:inputType="none" 
         android:textIsSelectable="true" 
         android:maxLines="1" 
         android:singleLine="true" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:id="@+id/occupation" 
         android:hint="Occupation" /> 
        </android.support.design.widget.TextInputLayout> 
        <LinearLayout 
         android:id="@+id/occupiSalaryHide" 
         android:layout_width="match_parent" 
         android:visibility="gone" 
         android:orientation="vertical" 
         android:layout_height="wrap_content"> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/companyName" 
          android:layout_weight="1" 
          android:hint="Company name" /> 
         </android.support.design.widget.TextInputLayout> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/monthlyIncome" 
          android:layout_weight="1" 
          android:hint="Gross monthly income" /> 
         </android.support.design.widget.TextInputLayout> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:focusable="false" 
          android:inputType="none" 
          android:textIsSelectable="true" 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/modeSalary" 
          android:layout_weight="1" 
          android:hint="Mode of salary" /> 
         </android.support.design.widget.TextInputLayout> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:focusable="false" 
          android:inputType="none" 
          android:textIsSelectable="true" 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/typeCompany" 
          android:layout_weight="1" 
          android:hint="Type of Company*" /> 
         </android.support.design.widget.TextInputLayout> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:focusable="false" 
          android:inputType="none" 
          android:textIsSelectable="true" 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/professionType" 
          android:layout_weight="1" 
          android:hint="Profession Type" /> 
         </android.support.design.widget.TextInputLayout> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:focusable="false" 
          android:inputType="none" 
          android:textIsSelectable="true" 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/designation" 
          android:layout_weight="1" 
          android:hint="Designation" /> 
         </android.support.design.widget.TextInputLayout> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:focusable="false" 
          android:inputType="none" 
          android:textIsSelectable="true" 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/noYearCurrWork" 
          android:layout_weight="1" 
          android:hint="Number of Years in Current Work" /> 
         </android.support.design.widget.TextInputLayout> 
         <android.support.design.widget.TextInputLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"> 

         <AutoCompleteTextView 
          android:focusable="false" 
          android:inputType="none" 
          android:textIsSelectable="true" 
          android:maxLines="1" 
          android:singleLine="true" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:id="@+id/noOfYears" 
          android:layout_weight="1" 
          android:hint="Total no. of years in work" /> 
         </android.support.design.widget.TextInputLayout> 

        </LinearLayout> 


        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_margin="10dp" 
         android:gravity="center"> 

         <TextView 
          android:id="@+id/applicationContinu" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center" 
          android:background="@drawable/button_border" 
          android:gravity="center" 
          android:clickable="true" 
          android:onClick="applicationValid" 
          android:padding="10dp" 
          android:text="Continue" 
          android:textColor="#fff" /> 
        </LinearLayout> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 

我有两个以上这样的上述这一点,但在中,onClick()是工作,但在这个不工作。在这setOnClickListener()正在工作,但我想用?

我做错了什么?

+0

显示您的java代码。 –

+0

你有什么问题?我不明白你的问题。哪个textview点击事件不起作用? –

+0

嗨Ankit,你似乎对android更新鲜。 正如你所要求的onclick方法,你必须分享你的java代码,让其他人查看,什么是问题。 – Khemraj

回答

0

设置点击任何视图,你可以按照这个。

假设你有TextView的

<TextView 
    android:id="@+id/tv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Application info"/> 

你可以设置你的java代码点击喜欢

public void setClick(){ 
    TextView textView = (TextView) findViewById(R.id.tv); 
    textView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     //    ... your code for click 
     } 
    }); 
} 

如果您使用的是活动的,那么你可以调用XML的方法类似

<TextView 
    android:id="@+id/tv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Application info" 
    android:onClick="yourMethodName..."/> 

请记住,xml的点击方式仅适用于活动。

+0

这是一个活动只,但在相同的XML,如果有类似的代码上面的代码,并且在onClick工作,但不工作这 –

+0

你可以使用两个解决方案我提到 如果你的ID是相同的你用xml。 – Khemraj

+0

@AnkitSrivastava分享您的java代码为onclick – Khemraj

相关问题