setonclicklistener在android中不工作

hmae6n7t  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(325)

我想申请一个 setOnClickListener
EditText View 在一个 Fragment 由于某些原因,它不起作用,当我单击 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;
    }
tsm1rwdh

tsm1rwdh1#

请改用getactivity().getapplicationcontext()

svdrlsy4

svdrlsy42#

编辑文本需要 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
你可以用 onClickListener 以及 onFocusChangeListener 一起:

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
    }
});
neekobn8

neekobn83#

EditText edText = view.findViewById(R.id.add_pay_time);
        edText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "aaaa", Toast.LENGTH_LONG).show();
                }
            });

相关问题