程方式使用OutlineBox创建TextInputLayout

程方式使用OutlineBox创建TextInputLayout

本文介绍了如何以编程方式使用OutlineBox创建TextInputLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create TextInputLayout with Widget.MaterialComponents.TextInputLayout.OutlinedBox style. I tried many ways but couldn't get the required result.Here is my code.

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);

我也尝试过:

TextInputLayout textInputLayout = new TextInputLayout(getActivity(),null,TextInputLayout.BOX_BACKGROUND_OUTLINE);

我想创建这样的视图.

I want to create view like this .

推荐答案

更新

感谢@MikeM.

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

You need to use TextInputLayout.setBoxBackgroundMode() method to use OutlineBox style

  • 设置框背景的模式(填充,轮廓或无).
  • 然后,您需要使用 常量

    Then you need to use TextInputLayout.BOX_BACKGROUND_OUTLINE) Constants

    示例代码

    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);
    
    
        }
    
    }
    

    输出

    基于此答案: https://stackoverflow.com/questions/3246447/如何在Android中以编程方式设置样式属性

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

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

    That's the reason that TextInputLayout does not programmatically accept setting the outline boxed style.

    这是简单的解决方案:

    您可以使用 LayoutInflater

    You can use LayoutInflater

    • 将布局XML文件实例化到其相应的View对象中.
    • Instantiates a layout XML file into its corresponding View objects.

    演示

    <?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>
    

    AndroidX(+ 用于Android的材料组件):

    AndroidX (+Material Components for Android):

    <?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>
    
    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);
        }
    }
    

    输出

    如果要从XML添加TextInputLayout,请查看以下答案:

    If you want to add a TextInputLayout from XML, then please check out the following answer:

    如果要以编程方式添加5个以上的TextInputLayout,请考虑使用RecyclerView.查看以下答案:

    If you want to add more than 5 TextInputLayouts programmatically, then please consider using a RecyclerView. Check out the following answers:

    • Dynamic form with repeating form
    • How can I validate recyclerview adapter TextInputEditText from fragment?

    希望这会有所帮助!

    这篇关于如何以编程方式使用OutlineBox创建TextInputLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:29