2

Как создать EditText(необязательно) , что бы он был на весь экран и с такими линиями?

Пример:

введите сюда описание изображения

P.S. При использовании кода в ответе Likhanov'a получается вот так: введите сюда описание изображения

Код немного изменён:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.x.myfirstnormal.ActivityAdd"
    android:background="#D6E100">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="2dp"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        android:backgroundTint="@android:color/black"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="52dp"
        android:text="Name*"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button6"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:layout_marginBottom="552dp"
        android:layout_marginLeft="168dp"
        android:backgroundTint="@android:color/holo_green_light"
        android:text="OK"
        android:textColor="@android:color/background_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <Button
        android:id="@+id/button7"
        android:layout_width="120dp"
        android:layout_height="52dp"
        android:layout_marginBottom="552dp"
        android:backgroundTint="@android:color/holo_red_light"
        android:text="Clear all"
        android:textColor="@android:color/background_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <EditText
        android:id="@+id/editText6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:ems="10"
        android:hint="Female"
        android:inputType="textPersonName"
        android:backgroundTint="@android:color/black"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/female" />

    <TextView
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:text="Female*"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        android:layout_marginLeft="32dp" />

    <!--<ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="147dp"
        tools:layout_editor_absoluteY="228dp">
-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="240dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.example.x.myfirstnormal.LinedEditText
            android:id="@+id/mainEditText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textMultiLine"
            android:ems="10"
            android:textColor="@color/Black"
            />
    </LinearLayout>
    <!--</ScrollView>-->

</android.support.constraint.ConstraintLayout>
1
  • 1
    @pavlofff Изменил вопрос. 14 янв 2018 в 11:57

1 ответ 1

5
<EditText
    android:inputType="textMultiLine"
    android:lines="8" <!-- приоритетное кол-во линий для отображения -->
    android:minLines="6" <!-- минимум линий -->
    android:gravity="top|left" <!-- позиция курсора -->
    android:maxLines="10" <!-- максимум линий -->
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:scrollbars="vertical"
/>

Если нужны линии в EditText то необходимо создать свой кастомный EditText Пример.

Создаете класс

public class LinedEditText extends EditText {

    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
      //  mPaint.setColor(getResources().getColor()); //SET YOUR OWN COLOR HERE
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //int count = getLineCount();

        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();//for long text with scrolling

        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);//first line

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();//next line
        }

        super.onDraw(canvas);
    }

}

И такой xml файл

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/multiline_exdittext_layout"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <YOUR_PACKAGE_NAME.LinedEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/edittxt_multilines"
        />

</LinearLayout>

Пример того, что получилось

Пример того, что получилось Источник

8
  • 2
    А как такие же линии сделать знаете? 14 янв 2018 в 12:27
  • 3
    а там какие получились? Такие же в плане стиля?
    – Likhanov
    14 янв 2018 в 12:29
  • 2
    получилось текстовое поле , без линий , просто текстовое поле. 14 янв 2018 в 12:30
  • 3
    похоже это возможно только созданием своего кастомного editText
    – Likhanov
    14 янв 2018 в 12:44
  • 4
    @True-hacker к сожалению не знаю почему у вас так. Я прикрепил к ответу свой скрин
    – Likhanov
    15 янв 2018 в 6:52

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.