简体   繁体   English

带有清除按钮的 Android java android.support.design.widget.TextInputLayout

[英]Android java android.support.design.widget.TextInputLayout with clear button

Is there any way to get something like that, plus the button to clean/delete the text when it contains something?有什么办法可以得到类似的东西,再加上在文本包含某些内容时清除/删除文本的按钮?

<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Inserisci Username">

                    <EditText
                        android:id="@+id/editText"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Username"
                        android:inputType="text" />

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

To clear the text from edit text从编辑文本中清除文本

@Override
public void onClick(View v) {
    editText.getText().clear(); 
    //or you can use editText.setText("");
}

To put the button inside edittext just make the custom layout要将按钮放在 edittext 中,只需进行自定义布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
>

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/oval"
    android:layout_centerInParent="true"
    >

    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@+id/id_search_button"
        android:hint="Inserisci Username">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="username"
            android:background="@android:color/transparent"
            android:inputType="text" />

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

    <ImageButton android:id="@+id/id_search_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:layout_centerVertical="true"
        android:background="@drawable/ic_clear_black"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

</RelativeLayout>

drawable oval可绘制的椭圆形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ffffff"/>
    <stroke android:width="2px" android:color="#ff00ffff"/>
    <corners android:radius="24dp" />
</shape>

To hide focus of the editText when app starts在应用程序启动时隐藏 editText 的焦点

add these lines inside activity tag of your class name in manifest file清单文件中类名的活动标签中添加这些行

<activity
        android:name=".yourActivityName"
        android:windowSoftInputMode="stateAlwaysHidden"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"您可以设置布局的属性,如android:descendantFocusability="beforeDescendants"android:focusableInTouchMode="true"

<RelativeLayout 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"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
xmlns:ads="http://schemas.android.com/apk/res-auto"
>

I know it's been a while, but maybe it helps someone.我知道已经有一段时间了,但也许它对某人有所帮助。

If you use Material components, you can use app:endIconMode="clear_text" , for example:如果使用 Material 组件,则可以使用app:endIconMode="clear_text" ,例如:

 <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/key_word_wrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:hintEnabled="true"
                android:hint="@string/key_word"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_gravity="center_horizontal"
                app:endIconMode="clear_text">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/key_word"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text" />
            </com.google.android.material.textfield.TextInputLayout>

This will bring up the little X button as soon as the text has been entered, and will automatically work as expected.这将在输入文本后立即显示小 X 按钮,并会自动按预期工作。

Read more options at Text fields文本字段中阅读更多选项

You could use onTouchEvent() for determine user touch to 'clear text' icon您可以使用onTouchEvent()来确定用户触摸“清除文本”图标

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        if (x >= (this.getRight() - drawableClearText.getBounds().width()) && x <= (this.getRight() - this.getPaddingRight())
                && y >= this.getPaddingTop() && y <= (this.getHeight() - this.getPaddingBottom())) {
            this.setText("");
            event.setAction(MotionEvent.ACTION_CANCEL);
        }
    }
    return super.onTouchEvent(event);
}

To manage the icon states(visible, invisible) you may use TextViewCompat.setCompoundDrawablesRelative(your_edit_text, compounds[0], compounds[1], drawableClearText, compounds[3])要管理图标状态(可见,不可见),您可以使用TextViewCompat.setCompoundDrawablesRelative(your_edit_text, compounds[0], compounds[1], drawableClearText, compounds[3])

暂无
暂无

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

相关问题 android.support.design.widget.TextInputLayout 无法实例化 - android.support.design.widget.TextInputLayout could not be instantiated android.support.design.widget.TextInputLayout-双重麻烦 - android.support.design.widget.TextInputLayout - double trouble java.lang.ClassCastException:android.support.v7.widget.AppCompatButton无法转换为android.support.design.widget.FloatingActionButton - java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.support.design.widget.FloatingActionButton java.lang.ClassCastException:android.support.design.widget.AppBarLayout无法转换为android.support.v7.widget.Toolbar - java.lang.ClassCastException: android.support.design.widget.AppBarLayout cannot be cast to android.support.v7.widget.Toolbar 未找到类Android支持设计小部件NavigationView - Class Not Found Android Support Design Widget NavigationView 添加android.support.design.widget.NavigationView时Java android崩溃 - Java android crash when add android.support.design.widget.NavigationView android.support.design.widget.TabLayout - 无法解析符号“设计” - android.support.design.widget.TabLayout - cannot resolve symbol 'design' 从android.support.design.widget.FloatingActionButton膨胀浮动操作按钮 - Inflate Floating Action Button from android.support.design.widget.FloatingActionButton Android Studio android.support.design.widget.CoordinatorLayout中的渲染问题 - Rendering issue in android studio android.support.design.widget.CoordinatorLayout Android Java 计算器(清除按钮) - Android Java Calculator (Clear Button)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM