Pau*_*aul 5 java android android-input-method android-edittext
有没有什么方法可以得到类似的东西,加上在文本包含某些内容时清理/删除文本的按钮?


<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>
Run Code Online (Sandbox Code Playgroud)
Zee*_*Zee 10
我知道已经有一段时间了,但也许它对某人有帮助。
如果您使用 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>
Run Code Online (Sandbox Code Playgroud)
一旦输入文本,就会弹出小 X 按钮,并且会自动按预期工作。
在文本字段中阅读更多选项
从编辑文本中清除文本
@Override
public void onClick(View v) {
editText.getText().clear();
//or you can use editText.setText("");
}
Run Code Online (Sandbox Code Playgroud)
要将按钮放在 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>
Run Code Online (Sandbox Code Playgroud)
可绘制椭圆形
<?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>
Run Code Online (Sandbox Code Playgroud)
应用程序启动时隐藏 editText 的焦点
将这些行添加到清单文件中类名的活动标记内
<activity
android:name=".yourActivityName"
android:windowSoftInputMode="stateAlwaysHidden"
android:focusable="true"
android:focusableInTouchMode="true"
/>
Run Code Online (Sandbox Code Playgroud)
您可以设置布局的属性,例如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"
>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6027 次 |
| 最近记录: |