【问题标题】:How to place image view beside editTtext view如何在editTtext视图旁边放置图像视图
【发布时间】:2020-04-17 14:17:33
【问题描述】:

我在不使用 textInputLayout 的情况下进行了密码可见性切换(因为我想要默认的 ediText 字段感觉)。

如何将 imageView 放置在布局中的 editText 旁边,单击该布局时会在我的 java 代码中执行密码可见性操作?

这是我目前拥有的..

我希望将眼睛图标放在右侧的 editText 字段中。

我的 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android.support.design="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center"
android:background="@color/white"
tools:context=".RegisterActivity"
android:fitsSystemWindows="true"
>
.......
<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/etPassword"
    android:layout_marginRight="20dp"
    android:inputType="textPassword"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    android:paddingLeft="20dp"
    android:background="@drawable/input_border"
    android:hint="Your Password" />

<ImageView
    android:id="@+id/show_pass_btn"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:alpha=".5"
    android:onClick="ShowHidePass"
    android:padding="5dp"
    android:src="@drawable/eye_show" />
    .......
   </LinearLayout>

【问题讨论】:

  • 不工作。布局扭曲,内容不显示

标签: java android xml android-layout


【解决方案1】:

[更新回复] 你可以使用 android:drawableRight="@drawable/eye_show" 见 sn-p。这会将图像放置在右侧,使用 android:drawableLeft="@drawable/eye_show" 将图像放置在 EditText 的左侧

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android.support.design="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@color/white"
    tools:context=".RegisterActivity"
    android:fitsSystemWindows="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/etPassword"
        android:layout_marginRight="20dp"
        android:inputType="textPassword"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="20dp"
        android:paddingLeft="20dp"
        android:background="@drawable/input_border"
        android:hint="Your Password"
       android:drawableRight="@drawable/eye_show" />


    </LinearLayout>


       </LinearLayout>

【讨论】:

  • 我做了这个,没用。图像未按需要放置在 editText 内
  • 工作,但是如何处理图像上的点击事件?图片有 onClick 属性,它调用我的 java 代码来显示/隐藏密码。
  • 我已经用这个stackoverflow.com/questions/3554377/…完成了
  • 但是点击图标后如何更改图像 - if(password.getTransformationMethod().equals(PasswordTransformationMethod.getInstance())){((ImageView)(view)).setImageResource(R .drawable.eye_hide); } 不工作 - EditText 不能投射到图像视图
  • 没关系。都修好了!!! with editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.drawableRight, 0);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 2023-02-15
  • 2020-02-12
  • 2011-12-15
  • 2016-01-06
  • 1970-01-01
相关资源
最近更新 更多