API 21提供了使用以下功能的方法吗:
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
我正在尝试使EditText提示浮动。
谢谢!
API 21提供了使用以下功能的方法吗:
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
我正在尝试使EditText提示浮动。
谢谢!
浮动提示的EditText:
在Gradle中添加以下依赖项:
compile 'com.android.support:design:22.2.0'
在布局中:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
</android.support.design.widget.TextInputLayout>
implementation 'com.google.android.material:material:1.0.0-rc01'
。 - rmtheisAndroid支持库可以在Gradle的依赖项中导入:
compile 'com.android.support:design:22.2.0'
它应该在Gradle中包含,请!以下是使用它的示例:
<android.support.design.widget.TextInputLayout
android:id="@+id/to_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextViewTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="To"
android:layout_marginTop="45dp"
/>
</android.support.design.widget.TextInputLayout>
顺便说一下,编辑器可能不理解在TextInputLayout中允许使用AutoCompleteTextView。
导入支持库。在您的项目的build.gradle文件中,在项目的依赖项中添加以下行:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
}
<android.support.design.widget.TextInputLayout
android:id="@+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Username"/>
</android.support.design.widget.TextInputLayout>
然后,在调用“setContentView”后,立即在“TextInputLayout”上调用“setHint”,因为要动画化浮动标签,只需要使用“setHint”方法设置提示即可。
final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);
usernameWrapper.setHint("Username");
setHint()
方法。它可以与在布局xml文件中设置的EditText
的提示一起使用。 - Ionoclast Brigham您需要将以下内容添加到模块的 build.gradle 文件中:
implementation 'com.google.android.material:material:1.0.0'
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/my_hint">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
</com.google.android.material.textfield.TextInputLayout>
@andruboy提供的https://gist.github.com/chrisbanes/11247418可能是您最好的选择。
https://github.com/thebnich/FloatingHintEditText在appcompat-v7 v21.0.0上有点用处,但是由于v21.0.0不支持EditText
子类的重音颜色,因此FloatingHintEditText
的下划线将是默认的纯黑色或白色。另外,内边距没有针对Material风格的EditText
进行优化,因此您可能需要调整它。
compile 'com.github.TeleClinic:SmartEditText:0.1.0'
然后你可以像这样做:
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="@+id/emailSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Email"
app:setMandatoryErrorMsg="Mandatory field"
app:setRegexErrorMsg="Wrong email format"
app:setRegexType="EMAIL_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="@+id/passwordSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Password"
app:setMandatoryErrorMsg="Mandatory field"
app:setPasswordField="true"
app:setRegexErrorMsg="Weak password"
app:setRegexType="MEDIUM_PASSWORD_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="@+id/ageSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Age"
app:setMandatory="false"
app:setRegexErrorMsg="Is that really your age :D?"
app:setRegexString=".*\\d.*" />
请使用Material Components Library提供的TextInputLayout
:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>