mut*_*nji 22 android android-textinputlayout
我想制作一个EditTextLayout,但我想要一个不同的标签和提示文字.
例如:标签的文本是"电话号码",提示文本是"+6281342134".
可能吗?
我的代码是:
<android.support.design.widget.TextInputLayout
android:id="@+id/phone_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="+6281342134"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
sta*_*ej2 40
我对Rehan采取了类似的方法
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
inputLayout.setHint("We did it!");
} else {
inputLayout.setHint("Testing 123");
}
}
});
Run Code Online (Sandbox Code Playgroud)
在不禁用动画的情况下,动画对我来说足够好了:
Sho*_*use 18
以下是我在没有代码(只是XML)的情况下执行此操作,同时同时允许标签和占位符,如在Material Design准则中.
布局XML:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="My Label Text">
<android.support.design.widget.TextInputEditText
android:id="@android:id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@drawable/hint_selector"
android:hint="Placeholder Text"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
颜色选择器XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="?android:textColorHint" />
<item android:color="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)
这里的关键是默认情况下使其透明,并且仅在具有焦点时显示占位符文本.无需改变颜色,因为这将尊重任何自定义主题,以及明暗主题.
您可以使用这些属性:
android:hint: 对于标签
placeholderText: 用于内部的占位符文本 EditText
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phone_layout"
android:layout_width="match_parent"
android:hint="Phone Number"
app:placeholderText="+6281342134"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="+6281342134"
android:inputType="phone" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
如果在文本字段为空时也应显示占位符,只需添加expandHintEnabled="false":
<com.google.android.material.textfield.TextInputLayout
app:expandedHintEnabled="false"
Run Code Online (Sandbox Code Playgroud)
注:该expandedHintEnabled要求至少版本1.3.0-alpha03。
| 归档时间: |
|
| 查看次数: |
14099 次 |
| 最近记录: |