Android EditText 带图标/按钮

Android EditText with icon/button

提问人:Ionut Ciuta 提问时间:2/9/2016 更新时间:9/28/2017 访问量:21665

问:

我怎样才能获得一个 as 那些 below?EditText

enter image description here

x 按钮删除插入的文本,眼睛按钮只要按下它就会显示清除密码。请注意,我将它们称为按钮,因为我真的不知道它们到底是什么,也不知道它们是否是 EditText 本身的一部分,或者它们是否是独立视图。

安卓 android-编辑文本

评论

1赞 Wasim K. Memon 2/9/2016
为此,您可以在 editText 中使用 drawableRight。搜索它,你会发现很多关于它的信息。
0赞 Ionut Ciuta 2/9/2016
@androidnoobdev感谢您提供的信息;我会查一下的
0赞 IntelliJ Amiya 2/9/2016
practicaldroid.blogspot.in/2012/08/......

答:

2赞 invisbo 2/9/2016 #1

您可以使用此:也用于单击此答案android:drawableRight="@drawable/your_icon"

可能会有所帮助

2赞 OBX 2/9/2016 #2

这是在末尾创建带有十字按钮的最佳方法:EditText

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp">

<EditText
    android:id="@+id/calc_txt_Prise"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"  
    android:layout_marginTop="20dp"
    android:textSize="25dp"
    android:textColor="@color/gray"
    android:textStyle="bold"
    android:hint="@string/calc_txt_Prise"
    android:singleLine="true" />

<Button
    android:id="@+id/calc_clear_txt_Prise"      
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_gravity="right|center_vertical"
    android:background="@drawable/delete" />

现在要清除 EditText 单击后,您可以使用

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditTextID.setText("");
        }
    });

为了使密码在按下按钮之前可见,您可以通过以下方式实现它:

yourButton.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

               switch ( event.getAction() ) {
                case MotionEvent.ACTION_DOWN: 
                   editText.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
                case MotionEvent.ACTION_UP: 
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
                }
                return true;
        }
    });
6赞 IntelliJ Amiya 2/9/2016 #3

你可以使用android:drawableRight

在 XML 中,在 EditText 中将 Drawable 设置到文本右侧

android:drawableRight="@drawable/Your_drawable"

评论

4赞 user2143491 1/27/2017
但是,您将如何获得 onClick 处理呢?
2赞 IntelliJ Amiya 1/8/2018
@user2143491 您可以关注 stackoverflow.com/a/26269435/3395198
9赞 Vasily Kabunov 2/9/2016 #4

您只需在 EditText 的右侧添加一个按钮即可

<FrameLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ScreenLoginContainer">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit_text_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:imeOptions="actionDone"
            android:hint="@string/enter_password_hint"
            android:paddingRight="30dp"
            style="@style/ScreenPasswordEditText"/>

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+screen_card_ean_enter/show_password_button"
        style="@style/ShowHidePasswordButton"/>

</FrameLayout>

评论

0赞 Ionut Ciuta 2/22/2016
这正是我想要的,尽管我用buttonImageView
2赞 rockgecko 9/28/2017 #5

设计支持库有一个方法:https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setPasswordVisibilityToggleEnabled(boolean)setPasswordVisibilityToggleEnabled

这对您的密码编辑文本有好处,但对用户名不利。