为什么onClick不能在我的代码中工作?

时间:2017-11-11 06:32:13

标签: android onclick textview cardview

TextView内有CardView。我想在点击Textview时发生一些功能,但我的android: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()正在运行,但我想使用android:onClick

我做错了吗?

1 个答案:

答案 0 :(得分: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点击方式仅适用于活动。