繁体   English   中英

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

[英]Android : Add a listener to an icon in a text field

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

在我的应用程序中(在 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
    }
});

由于您仅在 End 图标上明确设置侦听器,因此您不必担心验证 viewId。

首先,我在上面的代码中可以注意到,您正在尝试收听 View 上的点击事件

一个简单的方法是将您的onClick()方法更改为下面

public void onClick(View v) {
    
    if (v.getId() == R.id.start_date_Layout){
       
        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
    }
    
}

我相信我能帮上忙

如果您使用的是 EditText,请尝试将 onFocusChangeListner 设置为 editText 而不是 onClickListner。

startDateLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus) {
       // Show your code when has focus
    } else {
       // when it does not have focus
    }
}});

问题很简单,您可以使用FragmeLayoutLinearLayoutEditTextImageView来实现解决方案,它类似于TextInputEditText但您将对视图层次结构有更多的控制,让我向您展示如何实现它,为了简单起见,因为您是初学者,我将使用线性布局

创建一个FrameLayoutLinearLayout然后创建一个EditText和一个 ImageView 像

XML:

<LinearLayout
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#DCDFDF"
    android:orientation="horizontal"
    tools:ignore="MissingConstraints">


    <ImageView
        android:layout_gravity="center_vertical"
        android:id="@+id/imageViewTitle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:src="@drawable/android"
        tools:ignore="ContentDescription" />

    <EditText
        android:layout_gravity="center_vertical"
        android:id="@+id/editTextTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:background="#DCDFDF"
        android:gravity="top"
        android:hint="Start Date"
        android:paddingTop="10dp"
        android:textAlignment="gravity"
        android:importantForAutofill="no" />

</LinearLayout>

爪哇

public class MainActivity extends AppCompatActivity {
    EditText editTextTitle;
    ImageView imageViewTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextTitle = findViewById(R.id.editTextTitle);
        imageViewTitle = findViewById(R.id.imageViewTitle);

          imageViewTitle.setOnClickListener(new View.OnClickListener() {
          @Override
           public void onClick(View view) {
             //Do your work here
             Toast.makeText(MainActivity.this, "ImageView Pressed", Toast.LENGTH_SHORT).show();
           }
       });
    }
}

只需使用:

startDateLayout = v.findViewById(R.id.start_date_Layout);
startDateLayout.setEndIconOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // DO something....
    }
});

简单的科特林:

    b.dateLabel.setEndIconOnClickListener {
        Toast.makeText(requireContext(), "date", Toast.LENGTH_SHORT).show()
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM