Material 设计中的大纲编辑文本

Outlined Edit Text from Material Design

如何实现轮廓文本字段(如 this material design page 所示)?

阅读Outline Box

Outline text fields have a stroked border and are less emphasized. To use an outline text field, apply the following style to your TextInputLayout:

 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

依赖关系

implementation 'com.android.support:design:28.0.0-alpha1' 

XML

   <android.support.design.widget.TextInputLayout
   android:id="@+id/name_text_input"
   style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   >

   <android.support.design.widget.TextInputEditText
       android:id="@+id/name_edit_text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="@string/label_name" />
</android.support.design.widget.TextInputLayout>

DEMO

仅供参考

旧版支持在 Android 28 结束。现在继续使用

implementation 'com.google.android.material:material:1.3.0'

您需要将此依赖添加到您的 "module level" build.gradle com.google.android.material 中才能使用最新 material UI components.

在您的 com.google.android.material.textfield.TextInputLayout 中使用此样式,然后

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

here

结账

If you're using com.android.support:design library then you should change your app style to Theme.MaterialComponents...Bridge (I.e. like change style from Theme.AppCompat.Light to Theme.MaterialComponents.Light.Bridge)

first,

Next, you have to use this style in your android.support.design.widget.TextInputLayout:

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

更新

也能正常工作
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'

使用implementation 'com.android.support:design:28.0.0-alpha1' 我遇到错误

Cannot resolve symbol '@style/Widget.MaterialComponents.TextInputLayout.OutlineBox'

解决方案

Build.Gradle

中进行以下更改

使用compileSdkVersion 28

使用targetSdkVersion 28

使用下面的依赖项

implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support:design:28.0.0-alpha3'

示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.dangarmahesh.demoapp.MainActivity">

    <ImageView
        android:layout_width="250dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"
        android:layout_height="250dp" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/userIDTextInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"

        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/userIDTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="User ID" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/passwordTextInputLayout"
        android:layout_margin="10dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/passwordTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password" />

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


    <Button
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:text="LOGIN"
        android:textStyle="bold"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/white"
        android:layout_height="wrap_content" />
</LinearLayout>

OUTOUT

如果您正在使用 appcompact 库,那么您可以使用这个 android.support.design.widget.TextInputLayout

如果您使用的是 ANDROIDX build,那么根据 android jetpack,我得出一个结论:谁拥有最新代码。

要使用它,你需要在你的应用程序中有这个依赖项gradle

dependencies {
    implementation 'com.google.android.material:material:1.0.0'
}

然后您可以通过这种方式将 UI 元素添加到 xml

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/messageTextInputLayout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/passwordTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Text here" />

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

随着迁移到androidx库你必须使用新的Material Components for android library

使用TextInputLayout组件:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

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

并应用此样式:

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

要求:

  • 您必须在 build.gradle 文件中添加 this dependencies

    implementation 'com.google.android.material:material:<version>'

  • 申请一个Material Components theme

    <style name="Theme.MyApp" parent="Theme.MaterialComponents">

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="300dp"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:hint="First Name"/>
 </com.google.android.material.textfield.TextInputLayout>

在android-

中使用OutlinedTextField(与Jetpack Compose

            OutlinedTextField(
                                    modifier = Modifier.fillMaxWidth(),
                                    value = textState.value,
                                    onValueChange = {
                                        textState.value = it
                                        viewModel.setSteps(stepNo.plus(1), it.text)
                                    },
                                    label = {
                                        Text(text = "Step ".plus(stepNo.plus(1)).plus("..."))
                                    },
                                    shape = RoundedCornerShape(8.dp),
                                    colors = TextFieldDefaults.textFieldColors(
                                        backgroundColor = Color.Transparent
                                    ),
                                    trailingIcon = {
                                            IconButton(onClick = {}) {
                                            Icon(
                                                modifier = Modifier.padding(start = 10.dp),
                                                imageVector = Icons.Filled.Image,
                                                tint = Color.Blue,
                                                contentDescription = "Select Image"
                                            )
                                        }
                                    }
                                )

Jetpack Compose 示例代码- https://androidlearnersite.wordpress.com/2021/08/03/jetpack-compose-1-0-0-sample-codes/