首页 > 解决方案 > setOnClickListener 在片段 Android 中不起作用

问题描述

我正在尝试在 a 中应用 a setOnClickListenerEditText ViewFragment由于某种原因,它不起作用,当我单击EditText.

片段 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".add_payment">

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

        <TextView
            android:id="@+id/testpay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Time:"
            android:textColor="#000000"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/add_pay_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:ems="10"
            android:inputType="time" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Date:"
            android:textColor="#000000"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/add_pay_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="date" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Name:"
            android:textColor="#000000"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/add_pay_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Table:"
            android:textColor="#000000"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/add_pay_table"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Amount:"
            android:textColor="#000000"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/add_pay_amm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />
    </LinearLayout>

</LinearLayout>

片段代码:

@Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.add_payment_fragment, container, false);
        view.findViewById(R.id.add_pay_time).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

标签: javaandroidandroid-fragmentstextviewonclicklistener

解决方案


编辑文字需要focus调用onClick()

解决方案 1

设置监听器时请求焦点:

EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
    }
});
editText.requestFocus(); //request focus

解决方案 2

在触摸监听器上设置:

EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction())
            Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
        return false;
    }
});

解决方案 3

您可以一起使用onClickListeneronFocusChangeListener

EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showToast();//call your method
    }
});

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
            showToast();//call your method
    }
});

推荐阅读