Android 编辑文本和水平线

Android 编辑文本和水平线,android,android-edittext,Android,Android Edittext,当我们使用EditText键入输入值时,我希望在所有手机中类似于电话簿的输入应用程序下方放置一条水平线 输入电话:___________ 我想把我的输入放在这一行。如何使用EditText完成此操作?试试这个 <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_conten

当我们使用EditText键入输入值时,我希望在所有手机中类似于电话簿的输入应用程序下方放置一条水平线

输入电话:___________

我想把我的输入放在这一行。如何使用EditText完成此操作?

试试这个

 <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:layout_marginTop="2dp">

            <EditText
                android:id="@+id/Prac"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Practice"
                android:inputType="text"
                android:textSize="12sp" />
        </android.support.design.widget.TextInputLayout>


您必须将编辑文本的宽度从
match\u parent
设置为
wrap\u content
代码下的行

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
 <EditText
 android:layout_width="200dp"
 android:layout_height="wrap_content"
 android:backgroundTint="#000000"
 android:hint=" "  />

您必须将宽度设置为
匹配父项
或固定宽度,如
100dp

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
/>

一种方法是

使背景色为黑色,并设置如下代码所示的提示

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
 <EditText
 android:layout_width="200dp"
 android:layout_height="wrap_content"
 android:backgroundTint="#000000"
 android:hint=" "  />

另一种方法是,,
在editText下面添加一个黑色背景的视图,editText的背景设置为空。

正如每个人回答的那样,设置宽度和高度以包装内容这是正确的答案,但仍然是实现这一点的另一种方法让我们看一看

首先创建一个可绘制文件(myline.xml),用于创建线条

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:bottom="1dp"
    android:left="-2dp"
    android:right="-2dp"
    android:top="-2dp">
    <shape android:shape="rectangle">
        <stroke
            // You can change width according to your need
            android:width="1dp"
            android:color="#000000" />
    </shape>
</item>

现在在layout.xml中,在编辑文本背景中使用myline.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Phone: "
        android:textColor="#000"
        android:textSize="18dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/myline"
        android:textColor="#000"
        android:textSize="18dp" />
</LinearLayout>

完成后,请参见下面的屏幕截图中的输出


您可以尝试使用CustomEditText,如以下代码:

package com.cj.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * Created by E069099 on 7/20/2017.
 */

public class CustomEdiTextWithLine extends EditText {
    private Rect rect;
    private Paint paint;

    public CustomEdiTextWithLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        rect = new Rect();
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(2F);

        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setTextSize(20);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //start at the vertical center of the textview
        float top = (getHeight() + getPaddingTop() - getPaddingBottom())/2;
        //start at the left margin
        float left = getPaddingLeft();
        //we draw all the way to the right
        float right = getWidth() -getPaddingRight();
        //we want the line to be 2 pixel thick
        float bottom = getHeight()- getPaddingBottom();
        canvas.drawLine(0,bottom,right,bottom,paint);
        super.onDraw(canvas);
    }
}

EditText已经表现出这种行为,当输入的长度增加时,行的长度会变大。一开始没有排队。我想在程序的开头放一整行。您必须将EditText的宽度设置为
match\u parent
或其他值,如
100dp
@gktg1414查看我的答案。你也可以用这种方法实现这一点。只需复制粘贴我的ans就可以了Good@NileshRathod我理解了要求宽度为
匹配\u parent
的答案。当您编写
包装内容时
。很大的不同。@PriteshVadhiya和你应该注意到,我的答案实际上是从我的评论中抄来的,而我的评论比任何其他答案都要早。和其他贴出的答案不同