понедельник, 20 июля 2015 г.

EditText плаваючі заголовки: Бібліотека підтримки Android

edittext_floating_labels_title
Сьогодні, поговоримо про EditText Floating Labels в Android додатках.

Новий TextInputLayout дозволяє нам запаковувати EditText для можливості показувавти плаваючі заголовки над полями EditText. Такж ми маємо можливість показувати помилки під полем EditText. Коил форма EditText в фокусі, підказка піднімається над view в ліву частину.
Перш за все нам необхіжно додати бібліотеку або прописати залежність в файлі gradle.



  1. dependencies {
  2. compile 'com.android.support:design:22.2.0'
  3. }
dependencies {
    compile 'com.android.support:design:22.2.0'
}
Після цього оновлюємо файл strings.xml file в проекті.


  1. <resources>
  2. <string name="app_name">EditText Floating Labels Demo</string>
  3. <string name="username">Username</string>
  4. <string name="password">Password</string>
  5. <string name="sign_in">Sign In</string>
  6. <string name="login_error">Username can not be empty</string>
  7. </resources>
<resources>
    <string name="app_name">EditText Floating Labels Demo</string>
    <string name="username">Username</string>
    <string name="password">Password</string>
    <string name="sign_in">Sign In</string>
    <string name="login_error">Username can not be empty</string>
</resources>
Наступний наш крок - додати layout до проекту, над яким ми експерементуємо..

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingLeft="12dp"
  7. android:paddingRight="12dp"
  8. android:paddingTop="8dp"
  9. android:paddingBottom="8dp">

  10. <android.support.design.widget.TextInputLayout
  11. android:id="@+id/login_layout"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_marginTop="8dp">

  15. <EditText
  16. android:id="@+id/edit_text_email"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:inputType="textEmailAddress"
  20. android:hint="@string/username" />

  21. </android.support.design.widget.TextInputLayout>

  22. <android.support.design.widget.TextInputLayout
  23. android:id="@+id/password_layout"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_marginTop="8dp">

  27. <EditText
  28. android:id="@+id/edit_text_password"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:inputType="textPassword"
  32. android:hint="@string/password" />

  33. </android.support.design.widget.TextInputLayout>

  34. <Button
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:text="@string/sign_in"
  38. android:id="@+id/sing_in_button"
  39. android:layout_gravity="center_horizontal" />

  40. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">

        <EditText
            android:id="@+id/edit_text_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="@string/username" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/password_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">

        <EditText
            android:id="@+id/edit_text_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="@string/password" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_in"
        android:id="@+id/sing_in_button"
        android:layout_gravity="center_horizontal" />

</LinearLayout>
Останнім нашим кроком буде оновлення  activity.

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main_activity);
  6. }
  7. }
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }
}
Ми також можемо показувати повідомлення про помилку в TextInputLayout. Для цього ми мусимо викоритати метод setErrorEnabled(boolean) та setError(CharSequence). Як приклад ми будемо перевіряти поле login на наявність символів. Тепер нам треба оновити метод onCreate та імплементувати метод onFocusChange.

  1. public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener {

  2. TextInputLayout mUsernameLayout;
  3. EditText mUsername;
  4. EditText mPassword;

  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main_activity);

  9. mUsernameLayout = (TextInputLayout) findViewById(R.id.login_layout);
  10. mUsername = (EditText) findViewById(R.id.edit_text_email);
  11. mPassword = (EditText) findViewById(R.id.edit_text_password);

  12. mUsername.setOnFocusChangeListener(this);
  13. mPassword.setOnFocusChangeListener(this);
  14. }

  15. @Override
  16. public void onFocusChange(View v, boolean hasFocus) {
  17. if (v != mUsername && mUsername.getText().toString().isEmpty()) {
  18. mUsernameLayout.setErrorEnabled(true);
  19. mUsernameLayout.setError(getResources().getString(R.string.login_error));
  20. } else {
  21. mUsernameLayout.setErrorEnabled(false);
  22. }
  23. }
  24. }


edittext floating labels

Бачимо результат, мені здається дуже гарно.

Комментариев нет:

Отправить комментарий