Android java android.support.design.widget.TextInputLayout с кнопкой очистки - PullRequest
0 голосов
/ 06 ноября 2018

Есть ли способ получить что-то подобное, плюс кнопка для очистки / удаления текста, когда он содержит что-то?

<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>

Ответы [ 2 ]

0 голосов
/ 06 ноября 2018

Очистить текст от редактируемого текста

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

Чтобы поместить кнопку в текстовом редакторе, просто создайте пользовательский макет

<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>

отрисовка овал

<?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>

Чтобы скрыть фокус текста редактирования при запуске приложения

добавить эти строки в активность тег вашего имени класса в манифест файл

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

Вы можете установить свойство Layout как 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"
>
0 голосов
/ 06 ноября 2018

Вы можете использовать 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);
}

Для управления состояниями значков (видимых, невидимых) вы можете использовать TextViewCompat.setCompoundDrawablesRelative(your_edit_text, compounds[0], compounds[1], drawableClearText, compounds[3])

...