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

hc8w905p  于 2021-07-05  发布在  Java
关注(0)|答案(5)|浏览(274)

我是一个应用程序开发的初学者,我想问一个问题,我尽了最大努力在谷歌得到答案,但我失败了。所以,
在我的应用程序(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();
    }

}

吐司从来没有出现,如果条件从来都不是真的,我的问题是,我如何才能得到的看法,在witch的用户点击。谢谢你的阅读,我希望我已经尽可能清楚了。

jq6vz3qz

jq6vz3qz1#

只需使用:

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

hmae6n7t2#

如果使用的是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
    }
}});
2guxujil

2guxujil3#

我只做以下工作:

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

当您显式设置listener on end图标时,您不必担心验证viewid。

cnwbcb6i

cnwbcb6i4#

首先,我在上面的代码中注意到,您正在尝试侦听视图上的click事件
一个简单的方法是将onclick()方法更改为

public void onClick(View v) {

    if (v.getId() == R.id.start_date_Layout){

        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
    }

}

我相信我能帮上忙

4c8rllxm

4c8rllxm5#

问题很简单,你可以用一个fragmelayout或linearlayout,edittext和一个imageview来实现这个解决方案,它将类似于textinputeditext,但是你将对视图层次结构有更多的控制,让我来告诉你如何实现它,为了简单起见,因为你是一个初学者,我将使用线性布局
创建 FrameLayout 或者 LinearLayout 然后创建一个 EditText 像这样的图像视图
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>

java

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

相关问题