按钮OnClickListener无法触发

时间:2019-05-06 09:04:21

标签: android-layout onclicklistener

我有两个放置在linearloyout内的按钮,但我注意到onclick事件不适用于任何一个按钮,就像我在所有代码中总是做的一样,我正在onCreateView内调用setOnClickListener事件,但是由于某些原因我不知道,该事件没有触发。

我尝试调试它,但是我注意到它已经可以进入setOnClickListener了。

以下是我的框架xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.infosight.manibell.fragment.AddCommodities">


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:id="@+id/mainrl"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/bg_form_2"/>

        <!--<ScrollView-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="match_parent">-->

            <LinearLayout
                android:id="@+id/layoutupload"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#e8ecfa"
                android:orientation="vertical"
                android:padding="10dp" >


                <!-- To display picture taken -->

                <ImageView
                    android:id="@+id/imgPreview"
                    android:layout_width="fill_parent"
                    android:layout_height="300dp"
                    android:layout_marginTop="15dp"/>



                <TextView
                    android:id="@+id/txtPercentage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="15dp"
                    android:textColor="#4e5572"
                    android:textSize="30dp" />

                <ProgressBar
                    android:id="@+id/progressBar"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="20dp"
                    android:layout_marginBottom="35dp"
                    android:visibility="gone"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/btnback"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="false"
                        android:layout_weight="0.5"
                        android:text="Back"
                        />

                    <Button
                        android:id="@+id/btnUpload"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:text="Upload To Server"
                        />
                </LinearLayout>



            </LinearLayout>

        <!--</ScrollView>-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">



            <LinearLayout
                android:id="@+id/layoutform"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/spacing_large"
                android:background="#BFFFFFFF"
                android:minWidth="400dp"
                android:orientation="vertical"
                android:padding="@dimen/spacing_xlarge">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/spacing_large"
                    android:text="Add Commodities"
                    android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <!-- category spinner -->
                        <Spinner
                            android:id="@+id/sp_category"
                            android:layout_width="fill_parent"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:background="@android:color/transparent"
                            android:layout_weight="1"
                            style="@style/Widget.AppCompat.Spinner.Underlined"/>

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/baseline_textsms_black_18dp" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <!-- subcategory spinner -->
                        <Spinner
                            android:id="@+id/sp_subcategory"
                            android:layout_width="fill_parent"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:background="@android:color/transparent"
                            android:layout_weight="1"
                            style="@style/Widget.AppCompat.Spinner.Underlined"/>

                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/ic_photo_library_black_24dp" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="2dp">

                        <EditText
                            android:id="@+id/description"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:layout_weight="1"
                            android:background="@android:color/transparent"
                            android:hint="Description"
                            android:inputType="text"
                            android:singleLine="true" />

                    </android.support.design.widget.TextInputLayout>

                        <!--<ImageView-->
                            <!--android:layout_width="30dp"-->
                            <!--android:layout_height="30dp"-->
                            <!--android:src="@drawable/ic_form_country" />-->
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="2dp">

                        <EditText
                            android:id="@+id/input_price"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:layout_weight="1"
                            android:background="@android:color/transparent"
                            android:hint="Price"
                            android:inputType="text"
                            android:singleLine="true" />

                    </android.support.design.widget.TextInputLayout>

                        <!--<ImageView-->
                            <!--android:layout_width="30dp"-->
                            <!--android:layout_height="30dp"-->
                            <!--android:src="@drawable/baseline_payment_black_18dp" />-->
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/imageselectclk"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:layout_weight="1"
                            android:background="@android:color/transparent"
                            android:textColor="@color/colorAccent"
                            android:text="Select main Photo"/>


                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/outline_attachment_black_18dp" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>





                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/secondimageselectclk"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:layout_weight="1"
                            android:background="@android:color/transparent"
                            android:textColor="@color/colorAccent"
                            android:text="Select second Photo"/>


                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/outline_attachment_black_18dp" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/thirdimageselectclk"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:layout_weight="1"
                            android:background="@android:color/transparent"
                            android:textColor="@color/colorAccent"
                            android:text="Select third Photo"/>


                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/outline_attachment_black_18dp" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/fourthimageselectclk"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/spacing_xxlarge"
                            android:layout_weight="1"
                            android:background="@android:color/transparent"
                            android:textColor="@color/colorAccent"
                            android:text="Select fourth Photo"/>


                        <ImageView
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:src="@drawable/outline_attachment_black_18dp" />
                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginBottom="5dp"
                        android:background="@color/colorAccent" />
                </LinearLayout>


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/spacing_xlarge">

                    <com.balysv.materialripple.MaterialRippleLayout
                        style="@style/RippleStyleWhite"
                        android:id="@+id/butrpply"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/colorPrimary">

                        <Button
                            android:id="@+id/btn_Add"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/selector"
                            android:text="Add Commodity"
                            android:textColor="@android:color/white" />
                    </com.balysv.materialripple.MaterialRippleLayout>

                    <TextView
                        android:id="@+id/failed_reg"
                        android:layout_below="@+id/butrpply"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/colorPrimaryDark"
                        android:layout_margin="24dp"
                        android:textSize="16sp"/>


                    <ProgressBar
                        android:id="@+id/login_progress"
                        style="?android:attr/progressBarStyleLarge"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:indeterminate="false"
                        android:visibility="gone" />
                </RelativeLayout>

            </LinearLayout>

        </ScrollView>
    </RelativeLayout>
</FrameLayout>

有问题的两个按钮是btnback和btnUpload。 下面是onclick事件的Java代码

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ((MainActivity)getActivity()).getSupportActionBar().setTitle("Add Commodity");
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

        View view = inflater.inflate(R.layout.fragment_add_commodities, container, false);

        Button vwbtnback= (Button) view.findViewById(R.id.btnback);
        vwbtnback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                vw_layoutupload.setVisibility(LinearLayout.GONE);
                vw_layoutform.setVisibility(LinearLayout.VISIBLE);
            }
        });
        Button vwbtnUpload= (Button) view.findViewById(R.id.btnUpload);
        vwbtnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                vw_layoutupload.setVisibility(LinearLayout.GONE);
                vw_layoutform.setVisibility(LinearLayout.VISIBLE);
            }
        });



        return view;
    }

0 个答案:

没有答案