Android:向文本字段中的图标添加监听器

2023-12-30

我是应用程序开发的初学者,我喜欢问一个问题,我尽力在谷歌中找到答案,但我失败了。 所以,

在我的应用程序(在java中)中,我也使用文本字段:

  <com.google.android.material.textfield.TextInputLayout

        android:layout_marginBottom="20dp"
        android:id="@+id/start_date_Layout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        app:helperText="Start Date"
        app:endIconMode="custom"
        app:endIconDrawable="@drawable/ic_date"

        >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/start_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="dd/mm/yy"

            />

    </com.google.android.material.textfield.TextInputLayout>

我向我的结束图标添加了一个监听器,如下所示:

this.startDateLayout = v.findViewById(R.id.start_date_Layout);
this.startDateLayout.setEndIconOnClickListener(this);

我的问题是当我尝试获取用户单击的视图时:

 @Override
public void onClick(View v) {
    
    if (v.getParent() == this.startDateLayout){
       
        Toast.makeText(this.context, "click", Toast.LENGTH_LONG).show();
    }
    
}

toast 永远不会出现,并且 if 条件永远不会成立,我的问题是如何获取用户点击的视图。感谢您的阅读,我希望我尽可能清楚。


对我来说只做以下工作:

final TextInputLayout textInputLayout = findViewById(R.id.start_date_Layout);
textInputLayout.setEndIconOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // DO STUFF
    }
});

由于您仅在“结束”图标上显式设置侦听器,因此您不必担心验证 viewId。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:向文本字段中的图标添加监听器 的相关文章

随机推荐