如何通过 Java 处理 TextInputLayout
How to handle TextInputLayout through Java
我想制作一个用于路线规划的界面,该界面具有 2 个状态
1 - 输入起始地址和目标地址
2 - 通过 GPS 定位服务做同样的事情
我已经通过 XML 文件制作了内容布局,但由于我需要在它们之间动态切换,所以我尝试通过 Java 按钮事件设置相同的内容。但是每当我尝试通过 Java 设置带有 EditText 的 TextInputLayout 并尝试在我的 Android 模拟器上编译并启动它时,它是一个 Pixel 2 API lvl 28 它给了我一个java.lang.IllegalArgumentError 消息 'The style on this component requires your app theme to be Theme.AppCompat (or a descendant)'.
inputTop = new android.support.design.widget.TextInputLayout(getApplicationContext());
inputTop.setLayoutParams(new ConstraintLayout.LayoutParams(Constraints.LayoutParams.FILL_PARENT - 120, (int) convertDpToPx(getApplicationContext(), 45)));
inputTop.setX(convertDpToPx(getApplicationContext(), 174));
inputTop.setY(convertDpToPx(getApplicationContext(), 60));
inputTopInner = new EditText(getApplicationContext());
inputTopInner.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
inputTopInner.setGravity(Gravity.CENTER);
inputTopInner.setInputType(InputType.TYPE_CLASS_TEXT);
inputTopInner.setTextColor(Color.rgb(151,151,151));
inputTopInner.setTextSize(12);
inputTopInner.setHint("Standort");
inputTopInner.setEms(10);
inputTop.addView(inputTopInner);
layout.addView(viewTop);
layout.addView(inputTop);
有人知道如何制作这个 运行 就像输入到 XML 文件时那样吗?
我们的项目是这样的:
我们将 Parent 保留为 LinearLayout 并在该布局中添加视图
LiearLayout 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);
我想制作一个用于路线规划的界面,该界面具有 2 个状态 1 - 输入起始地址和目标地址 2 - 通过 GPS 定位服务做同样的事情
我已经通过 XML 文件制作了内容布局,但由于我需要在它们之间动态切换,所以我尝试通过 Java 按钮事件设置相同的内容。但是每当我尝试通过 Java 设置带有 EditText 的 TextInputLayout 并尝试在我的 Android 模拟器上编译并启动它时,它是一个 Pixel 2 API lvl 28 它给了我一个java.lang.IllegalArgumentError 消息 'The style on this component requires your app theme to be Theme.AppCompat (or a descendant)'.
inputTop = new android.support.design.widget.TextInputLayout(getApplicationContext());
inputTop.setLayoutParams(new ConstraintLayout.LayoutParams(Constraints.LayoutParams.FILL_PARENT - 120, (int) convertDpToPx(getApplicationContext(), 45)));
inputTop.setX(convertDpToPx(getApplicationContext(), 174));
inputTop.setY(convertDpToPx(getApplicationContext(), 60));
inputTopInner = new EditText(getApplicationContext());
inputTopInner.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
inputTopInner.setGravity(Gravity.CENTER);
inputTopInner.setInputType(InputType.TYPE_CLASS_TEXT);
inputTopInner.setTextColor(Color.rgb(151,151,151));
inputTopInner.setTextSize(12);
inputTopInner.setHint("Standort");
inputTopInner.setEms(10);
inputTop.addView(inputTopInner);
layout.addView(viewTop);
layout.addView(inputTop);
有人知道如何制作这个 运行 就像输入到 XML 文件时那样吗?
我们的项目是这样的:
我们将 Parent 保留为 LinearLayout 并在该布局中添加视图
LiearLayout 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);