Him*_*bra 6 android android-layout android-edittext android-textinputlayout textinputlayout
我面临一个奇怪的问题,我有一个InputTextLayout和一个EditText,并且我正在尝试实现类似的功能(如下图所示)(来自材料设计指南的图像:https : //material.io/guidelines/components /text-fields.html#text-fields-layout),其中有两个单独的提示。我这样做是通过将android:hint添加到两个布局中。这可以正常工作,但是当焦点移开该位置时,“标签”将向下移动并与“占位符”文本重叠。(仅当用户未提供任何输入且编辑文本为空时,两个提示才重叠)。关于如何解决此问题的任何指示?
作为一项要求,我需要两个提示都存在,并且“标签”不应向下移至焦点更改。两种提示都应保持在原位
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
Aeg*_*gir 15
dependencies {
// ...
implementation 'com.google.android.material:material:1.2.0-alpha03'
// ...
}
<com.google.android.material.textfield.TextInputLayout
...
app:placeholderText="Placeholder text">
Run Code Online (Sandbox Code Playgroud)
完美一号!!!
xml代码
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayoutPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Phone Number">
<android.support.design.widget.TextInputEditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="+91"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
科特林代码
edtPhone.hint=""
edtPhone.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
inputLayoutPhone.isHintEnabled = hasFocus
if(hasFocus){
edtPhone.hint="+91"
}else{
edtPhone.hint= "Phone Number"
}
}
Run Code Online (Sandbox Code Playgroud)
Java代码
edtPhone.setHint("");
edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
edtPhone.setHint("+91");
}else{
edtPhone.setHint("Phone Number");
}
}
});
Run Code Online (Sandbox Code Playgroud)
小智 1
据我所知,我们不能如你所愿。
因为,TextInputLayout 的设计是在提示获得焦点后将其浮动,因此,一旦提示上升,占位符中将不再有任何内容。我们可以通过以下轻微更改来满足您的要求。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.stackoverflow.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Label"
android:textColor="@color/colorPrimary" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
app:hintEnabled="false">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:hint="Place Holder"
/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4353 次 |
最近记录: |