java - setOnClickListener 在 Android fragment 中不起作用

标签 java android android-fragments textview onclicklistener

我正在尝试在 Fragment 中的 EditText View 上应用 setOnClickListener ,出于某种原因,它不起作用,当我单击 EditText 时,监听器中的代码不会执行。

fragment 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>

fragment 代码:

@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;
    }

最佳答案

编辑文本需要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
    }
});

关于java - setOnClickListener 在 Android fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59905594/

相关文章:

android - 导入 com.google.android.gms.common.api.GoogleApiClient;无法解决

android - 我在对话框 fragment 内单击按钮后,应用程序立即崩溃

java - 在我的 FragmentContainerView 对象中找不到方法 getFragment()

Android 相互 fragment 化

java - map Activity 在 getFragmentById 上崩溃

java - 在 SQL 或 CODE 中更好地对聚合数据进行分组(就性能而言)

java - 在 Spring Boot 中使用 JWT 进行简单例份验证

java - 将 BufferedImage 转换为另一种类型

java - 为什么名单必须最终公布

java - 重试 asynctask 的最佳 java 实践 (android)