android - 如何以编程方式使用 OutlineBox 创建 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);
我也试过:
TextInputLayout textInputLayout = new TextInputLayout(getActivity(),null,TextInputLayout.BOX_BACKGROUND_OUTLINE);
解决方案
更新
感谢@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);
}
}
输出
- 当前不支持动态样式更改。您必须在创建视图之前设置样式(在 XML 中)。
这就是TextInputLayout
不以编程方式接受设置轮廓盒装样式的原因。
这是简单的解决方案:
您可以使用LayoutInflater
- 将布局
XML
文件实例化为其对应的View
对象。
演示
创建新布局
<?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 的材料组件):
<?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>
现在在您需要的布局中使用
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);
}
}
输出
笔记
如果TextInputLayout
要从 XML 添加,请查看以下答案:
如果您想以编程方式添加超过 5TextInputLayout
秒,请考虑使用RecyclerView
. 查看以下答案:
希望这可以帮助!
推荐阅读
- ios - 我无法在标签上打印我的计算结果
- kibana - 如何获取 Kibana 仪表板 ID?
- android - 在 React Native 中保存时旋转图像的问题
- html - 将视频标签标签数据保存到 blob 而不损失分辨率
- ios - 将 Facebook 图像设置为按钮背景
- javascript - jQuery克隆表行
- azure-devops - 在 Azure DevOps 中是否有一种 GUI 方法可以禁用基于 YAML 的管道中的任务?
- google-cloud-platform - 无法导入图像数据集 gcloud automl,错误:标签已经是基本事实
- c# - 如何在 c# 中使用正则表达式获取某个特定单词之前的数字?
- java - NullPointerException:尝试调用虚拟方法'void org.apache.cordova.CordovaPlugin.privateInitialize