Czy API 21 zapewnia metodę korzystania z następującej funkcji:
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
Próbuję unosić wskazówki EditText.
Dzięki!
Czy API 21 zapewnia metodę korzystania z następującej funkcji:
http://www.google.com/design/spec/components/text-fields.html#text-fields-floating-labels
Próbuję unosić wskazówki EditText.
Dzięki!
Odpowiedzi:
Musisz dodać następujące elementy do pliku build.gradle modułu:
implementation 'com.google.android.material:material:1.0.0'
I użyj com.google.android.material.textfield.TextInputLayout w swoim kodzie XML:
<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>
Pływająca wskazówka EditText:
Dodaj poniżej zależność w gradle:
compile 'com.android.support:design:22.2.0'
W układzie:
<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>
22.2.0
implementation 'com.google.android.material:material:1.0.0-rc01'.
Tak, od 29 maja 2015 r. Ta funkcja jest teraz dostępna w bibliotece Android Design Support Library
Ta biblioteka obejmuje obsługę
Bibliotekę obsługi Androida można zaimportować w gradle w zależnościach:
compile 'com.android.support:design:22.2.0'
Powinien być zawarty w GradlePlease! I jako przykład, aby go użyć:
<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>
Przy okazji, edytor może nie rozumieć, że AutoCompleteTextView jest dozwolone w TextInputLayout.
Android nie zapewnia metody natywnej. Ani AppCompat.
Wypróbuj tę bibliotekę: https://github.com/rengwuxian/MaterialEditText
To może być to, czego chcesz.
Zaimportuj biblioteki pomocy technicznej, w pliku build.gradle projektu dodaj następujące wiersze w zależnościach projektu:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
}
Użyj następującego TextInputLayout w układzie interfejsu użytkownika:
<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>
Następnie wywołaj setHint na TextInputLayout zaraz po wywołaniu setContentView, ponieważ aby animować pływającą etykietę, wystarczy ustawić wskazówkę, używając metody setHint.
final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);
usernameWrapper.setHint("Username");
setHint(). Działa z podpowiedzią ustawioną EditTextw formacie XML układu.
Sugestia @ andruboy dotycząca https://gist.github.com/chrisbanes/11247418 jest prawdopodobnie najlepszym rozwiązaniem.
https://github.com/thebnich/FloatingHintEditText działa z appcompat-v7 v21.0.0, ale ponieważ wersja 21.0.0 nie obsługuje kolorów akcentujących z podklasami EditText, podkreślenie FloatingHintEditTextbędzie domyślną jednolitą czernią lub bielą. Również wyściółka nie jest zoptymalizowana pod kątem stylu Materiał EditText, więc może być konieczne jej dostosowanie.
Nie, nie ma. Spodziewałbym się tego w przyszłym wydaniu API, ale na razie utknęliśmy w EditText. Inną opcją jest ta biblioteka:
https://github.com/marvinlabs/android-floatinglabel-widgets
Aby ułatwić korzystanie z InputTextLayout, stworzyłem tę bibliotekę, która skraca twój kod XML do mniej niż połowy, a także zapewnia możliwość ustawienia komunikatu o błędzie, a także komunikatu ze wskazówką i łatwego sposobu walidacje. https://github.com/TeleClinic/SmartEditText
Po prostu dodaj
compile 'com.github.TeleClinic:SmartEditText:0.1.0'
Następnie możesz zrobić coś takiego:
<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.*" />
Skorzystaj z TextInputLayoutudostępnionych przez Bibliotekę komponentów materiałów :
<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>