如何在Android中使用TextInputLayout与EditText。

huangapple go评论72阅读模式
英文:

How to use TextInputLayout with EditText in Android

问题

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ui.login.send_phone.SendPhonePage">

    <ImageView
        android:id="@+id/sendPhone_logo"
        android:layout_width="@dimen/_150mdp"
        android:layout_height="@dimen/_150mdp"
        android:layout_marginTop="@dimen/_30mdp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@mipmap/ic_launcher_round" />

    <!--Phone number-->
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/signInFrag_phoneInpLay"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_15mdp"
        android:layout_marginLeft="@dimen/_15mdp"
        android:layout_marginEnd="@dimen/_15mdp"
        android:layout_marginRight="@dimen/_15mdp"
        app:boxStrokeColor="@color/colorAccent"
        android:layout_marginTop="@dimen/_30mdp"
        app:boxStrokeWidth="@dimen/_1mdp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/sendPhone_logo">

        <EditText
            android:id="@+id/signInFrag_phoneEdt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:gravity="left|center_vertical"
            android:hint="@string/insertYourPhoneNumber"
            android:inputType="phone"
            android:maxLength="11"
            android:maxLines="1"
            android:paddingLeft="@dimen/_10mdp"
            android:paddingRight="@dimen/_10mdp"
            android:singleLine="true"
            android:textColorHint="@color/colorLightGray"
            android:textSize="@dimen/_10mdp"
            app:drawableRightCompat="@drawable/ic_outline_phone" />

    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Please note that I've only translated the XML code for your layout and removed any surrounding text. If you have any further questions or need assistance, feel free to ask.

英文:

In my application I want use TextInputLayout, and for this I write below codes.<br>
But after run application, not show any stroke for Edit Text!<br>
Just show me empty Edit text!<br>
My result image : <br>
如何在Android中使用TextInputLayout与EditText。

My codes:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.ui.login.send_phone.SendPhonePage&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/sendPhone_logo&quot;
android:layout_width=&quot;@dimen/_150mdp&quot;
android:layout_height=&quot;@dimen/_150mdp&quot;
android:layout_marginTop=&quot;@dimen/_30mdp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
android:src=&quot;@mipmap/ic_launcher_round&quot; /&gt;
&lt;!--Phone number--&gt;
&lt;com.google.android.material.textfield.TextInputLayout
android:id=&quot;@+id/signInFrag_phoneInpLay&quot;
style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;@dimen/_15mdp&quot;
android:layout_marginLeft=&quot;@dimen/_15mdp&quot;
android:layout_marginEnd=&quot;@dimen/_15mdp&quot;
android:layout_marginRight=&quot;@dimen/_15mdp&quot;
app:boxStrokeColor=&quot;@color/colorAccent&quot;
android:layout_marginTop=&quot;@dimen/_30mdp&quot;
app:boxStrokeWidth=&quot;@dimen/_1mdp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/sendPhone_logo&quot;&gt;
&lt;EditText
android:id=&quot;@+id/signInFrag_phoneEdt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:background=&quot;@android:color/transparent&quot;
android:gravity=&quot;left|center_vertical&quot;
android:hint=&quot;@string/insertYourPhoneNumber&quot;
android:inputType=&quot;phone&quot;
android:maxLength=&quot;11&quot;
android:maxLines=&quot;1&quot;
android:paddingLeft=&quot;@dimen/_10mdp&quot;
android:paddingRight=&quot;@dimen/_10mdp&quot;
android:singleLine=&quot;true&quot;
android:textColorHint=&quot;@color/colorLightGray&quot;
android:textSize=&quot;@dimen/_10mdp&quot;
app:drawableRightCompat=&quot;@drawable/ic_outline_phone&quot; /&gt;
&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

I used this dependency : implementation &#39;com.google.android.material:material:1.2.1&#39; with this style &lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.Light.NoActionBar&quot;&gt;

How can i fix it?

答案1

得分: 0

请使用 TextInputEditText 代替 EditText,并删除 android:background="@android:color/transparent"

英文:

Use a TextInputEditText instead of EditText and remove the android:background=&quot;@android:color/transparent&quot;.

huangapple
  • 本文由 发表于 2020年10月9日 21:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64280879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定