如何以编程方式使用OutlineBox创建TextInputLayout

Dee*_*yee 11 android android-layout android-styles material-design textinputlayout

我想用Widget.MaterialComponents.TextInputLayout.OutlinedBox样式创建TextInputLayout.我尝试了很多方法,但无法获得所需的结果.这是我的代码.

TextInputLayout textInputLayout = new TextInputLayout(getActivity(),null,R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
textInputLayout.setHint("My Hint");
TextInputEditText editText = new TextInputEditText(textInputLayout.getContext());
textInputLayout.addView(editText);
parentView.addView(textInputLayout);
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

TextInputLayout textInputLayout = new TextInputLayout(getActivity(),null,TextInputLayout.BOX_BACKGROUND_OUTLINE);
Run Code Online (Sandbox Code Playgroud)

我想创建这样的视图.在此输入图像描述

Nil*_*hod 17

UPDATE

感谢@Mike M.

您需要使用TextInputLayout.setBoxBackgroundMode()方法来使用OutlineBox样式

setBoxBackgroundMode (int boxBackgroundMode)

  • 设置框背景的模式(填充,轮廓或无).

然后你需要使用TextInputLayout.BOX_BACKGROUND_OUTLINE)常量

注意:要获得TextInputLayout的OutlineBox中的角,您需要使用setBoxCornerRadii()方法

示例代码

public class MainActivity extends AppCompatActivity {

    LinearLayout parentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        parentView = findViewById(R.id.parentView);

        TextInputLayout emailTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

        emailTextInputLayout.setHint("Please Enter Email Address");
        emailTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
        emailTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
        TextInputEditText edtEmail = new TextInputEditText(emailTextInputLayout.getContext());
        emailTextInputLayout.addView(edtEmail);

        parentView.addView(emailTextInputLayout);


        TextInputLayout passTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

        passTextInputLayout.setHint("Please Enter Password");
        passTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
        passTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
        TextInputEditText edtPass = new TextInputEditText(passTextInputLayout.getContext());
        passTextInputLayout.addView(edtPass);

        parentView.addView(passTextInputLayout);


    }

}
Run Code Online (Sandbox Code Playgroud)

OUTPUT

在此输入图像描述

根据这个答案:https://stackoverflow.com/questions/3246447/how-to-set-the-style-attribute-programmatically-in-android

  • 目前不支持动态样式更改.您必须在创建视图之前设置样式(以XML格式).

这就是TextInputLayout没有以编程方式接受设置轮廓框样式的原因.

这是一个简单的解决方案:

您可以使用 LayoutInflater

  • 将布局XML文件实例化为其对应的View对象.

DEMO

创建一个新布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/userIDTextInputLayout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/userIDTextInputEditText"
        android:layout_width="match_parent"
        android:hint="Enter User Name"
        android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

AndroidX(适用于Android的 + Material Components):

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/userIDTextInputLayout"
    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/userIDTextInputEditText"
        android:layout_width="match_parent"
        android:hint="Enter User Name"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

现在使用在您所需的布局中LayoutInflater添加TextInputLayout

public class MainActivity extends AppCompatActivity {

    LinearLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rootView = findViewById(R.id.rootView);

        View view = LayoutInflater.from(this).inflate(R.layout.temp_layout, null);
        TextInputLayout userNameIDTextInputLayout=view.findViewById(R.id.userIDTextInputLayout);
        TextInputEditText userNameInputEditText = view.findViewById(R.id.userIDTextInputEditText);
        userNameIDTextInputLayout.setHint("Please Enter User Name");
        rootView.addView(view);
    }
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT

带有轮廓文本输入的应用程序截图

注意

如果要添加TextInputLayoutXML,请查看以下答案:

如果您想以TextInputLayout编程方式添加超过5 秒,请考虑使用RecyclerView.看看以下答案:

希望这可以帮助!

  • 嘿,尼勒什.仅供参考,最新的Material Components`TextInputLayout`具有`setBoxBackgroundMode()`方法,但它似乎没有被添加到[docs](https://developer.android.com/reference/com/google/android/material/textfield/TextInputEditText)呢.我们现在可以使用`setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE)`以编程方式严格执行此操作.我刚试过它,如果你想更新你的答案,这一切都很好.使用`com.google.android.material:material:1.1.0-alpha02`,顺便说一句. (2认同)