如何在viewPager中的Fragment中设置onClickListener?

时间:2018-01-06 18:31:30

标签: android android-fragments android-viewpager onclicklistener

我有3个片段的活动。我在Fragment thanbk和View Pager之间导航

在第一个片段中,我想要一个可点击的textView(“搜索”)和一个普通的editText(question_text) onclick(“搜索”)上的textView,我想将editText(question_text)可见性设置为“已消失”。 我从Stackoverflow尝试了很多方法,但我无法让它工作...... 应该很容易!

我哪里错了?

Heres是我的代码:

public class AskUsFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public AskUsFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @return A new instance of fragment AskUsFragment.
 */
public static AskUsFragment newInstance() {
    AskUsFragment fragment = new AskUsFragment();
    Bundle args = new Bundle();
    //args.putString(ARG_PARAM1, param1);
    //args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

FloatingActionButton askQuestionFAB;
private TextView search;
private EditText question_text;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}



@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_ask_us, container, false);
    askQuestionFAB = (FloatingActionButton)v.findViewById(R.id.submit_question);

    search = (TextView)v.findViewById(R.id.btn_input_search);
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            question_text = (EditText)v.findViewById(R.id.ask_question_text);
            question_text.getVisibility();
            question_text.setVisibility(View.GONE);
        }});

这是fragmentAskus.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ask_us_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black_a185"
android:theme="@style/Theme.AppCompat.Translucent"
tools:context="com.example.philipp.us.com.example.philipp.android.AskUsFragment">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/container_btn_nav"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginStart="140dp"
        android:layout_marginTop="280dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/btn_input_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_button_rectangle_rounded_corners_white"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:onClick="onUpdateLocation"
            android:padding="10dp"
            android:text="@string/search_text"
            android:textAlignment="center"
            android:textColor="@color/colorHintText"
            android:textSize="16sp"
            android:textStyle="bold" />

    </LinearLayout>

    <ImageView
        android:id="@+id/anim_swipe_right"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="280dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/anim_swipe_right" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/ask_us_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/white_transparent"
        android:visibility="invisible">

    </FrameLayout>

    <LinearLayout
        android:id="@+id/image_preview_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="17"
        android:background="@android:color/transparent"
        android:elevation="5dp"
        android:gravity="start"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image_preview0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:cropToPadding="true"
            android:onClick="onTakePicture0"
            android:scaleType="centerCrop"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/image_preview1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_marginEnd="3dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_marginStart="3dp"
            android:layout_weight="1"
            android:cropToPadding="true"
            android:onClick="onTakePicture1"
            android:scaleType="centerCrop"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/image_preview2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:cropToPadding="true"
            android:onClick="onTakePicture2"
            android:scaleType="centerCrop"
            android:visibility="invisible" />

    </LinearLayout>

    <EditText
        android:id="@+id/ask_question_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/rectangle_rounded_corners_white"
        android:gravity="top|center_vertical"
        android:hint="@string/hint_questionfield_fragment_ask_us"
        android:imeOptions="actionNext"
        android:inputType="text|textCapSentences|textAutoCorrect|textAutoComplete|textMultiLine"
        android:lines="4"
        android:maxLines="4"
        android:minLines="2"
        android:nextFocusDown="@+id/keyword0"
        android:paddingBottom="5dp"
        android:paddingEnd="20dp"
        android:paddingStart="20dp"
        android:paddingTop="10dp"
        android:scrollHorizontally="false"
        android:shadowColor="@color/black"
        android:singleLine="false"
        android:textColor="@color/black"
        android:textColorHint="@color/colorHintText"
        android:textSize="20sp" />

1 个答案:

答案 0 :(得分:1)

question_text = (EditText)v.findViewById(R.id.ask_question_text);放在听众之外。

public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_ask_us, container, false);
        question_text = (EditText) v.findViewById(R.id.ask_question_text);

        search = (TextView) v.findViewById(R.id.btn_input_search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                question_text.setVisibility(View.GONE);
            }
        });

否则,它会使用onClick()的View参数来查找ask_question_text