简体   繁体   English

带有图标/按钮的Android EditText

[英]Android EditText with icon/button

How can I obtain an EditText as those bellow? 我怎样才能获得如下的EditText

在此输入图像描述

The x button removes the text inserted and the eye button displays the clear password as long as it's pressed. x按钮删除插入的文本,只要按下,眼睛按钮就会显示清除密码。 Note that I refer to them as buttons because I don't really know what they actually are nor if they are part of the EditText itself or if they are independent views. 请注意,我将它们称为按钮,因为我不知道它们实际上是什么,也不知道它们是EditText本身的一部分还是它们是独立的视图。

You can just add a button at the right of your EditText 您只需在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>

You can use android:drawableRight 你可以使用android:drawableRight

Set a Drawable to the right of the text , in EditText , in XML . EditText中以XML格式在文本右侧设置Drawable

android:drawableRight="@drawable/Your_drawable"

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

This would be good for your password edittext, but not the username one. 这对您的密码edittext有好处,但不适用于用户名。

This is the prefect way to create an EditText with cross button at the end: 这是在结尾处创建带有十字按钮的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" />

And now to clear the EditText upon click you can use 现在,在点击后清除EditText即可使用

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

And in order to make the password visible till the button is pressed, you can implement it this way: 并且为了在按下按钮之前使密码可见,您可以这样实现:

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

You can use this : android:drawableRight="@drawable/your_icon" Also for click this answer 你可以使用这个: android:drawableRight="@drawable/your_icon"也可以点击这个答案

may help 可能有帮助

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

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