Android 文本输入布局错误右对齐
我有一个包含在TextInputLayout中的EditText。我希望在编辑文本下显示错误,但与屏幕右端对齐 这就是我目前拥有的: 错误如下所示: 我希望它看起来像什么: 我的XML是这样的:Android 文本输入布局错误右对齐,android,layout,textinputlayout,Android,Layout,Textinputlayout,我有一个包含在TextInputLayout中的EditText。我希望在编辑文本下显示错误,但与屏幕右端对齐 这就是我目前拥有的: 错误如下所示: 我希望它看起来像什么: 我的XML是这样的: <android.support.design.widget.TextInputLayout android:id="@+id/text_input_email" style="@style/forgot_pass_text_i
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_email"
style="@style/forgot_pass_text_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_enter_email_message"
android:layout_marginTop="@dimen/email_padding_top"
android:hintTextAppearance="@{forgotPasswordVM.hintTextAppearance}"
android:theme="@style/forgot_pass_til_state"
app:error="@{forgotPasswordVM.email.textInputError}">
<EditText
android:id="@+id/actv_email"
style="@style/forgot_pass_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email_address"
android:imeActionId="@+id/btn_submit"
android:imeOptions="actionSend"
android:inputType="textEmailAddress"
android:maxLines="1"
android:onEditorAction="@{forgotPasswordVM.onEditorAction}"
android:singleLine="true"
app:binding="@{forgotPasswordVM.email.text}" />
</android.support.design.widget.TextInputLayout>
。即使这样对我也不管用
我不知道还能尝试什么。请提出建议。多亏了你,我才想出了解决办法
我创建了一个自定义类:
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);
if (!enabled) {
return;
}
try {
Field errorViewField = TextInputLayout.class.getDeclaredField("mErrorView");
errorViewField.setAccessible(true);
TextView errorView = (TextView) errorViewField.get(this);
if (errorView != null) {
errorView.setGravity(Gravity.RIGHT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.END;
errorView.setLayoutParams(params);
}
}
catch (Exception e) {
// At least log what went wrong
e.printStackTrace();
}
}
}
现在我只是用CustomTextInputLayout替换了我的TextInputLayout。
工作起来很有魅力。谢谢。我希望这个答案能给你更多的信任。我的定制课程在科特林
class CustomTextInputLayout(context: Context, attrs: AttributeSet) :
TextInputLayout(context, attrs) {
override fun setErrorEnabled(enabled: Boolean) {
super.setErrorEnabled(enabled)
if (!enabled) {
return
}
try {
val layout = this
val errorView : TextView = ((this.getChildAt(1) as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(0) as TextView
(layout.getChildAt(1) as ViewGroup).layoutParams.width = LayoutParams.MATCH_PARENT
(layout.getChildAt(1) as ViewGroup).getChildAt(0).layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT
errorView.gravity = Gravity.END
} catch (e: Exception) {
e.printStackTrace()
}
}
}
如果选择此选项,则答案无效。另一个解决方案如下:
创建自定义类,如下所示:
public class CustomTextInputLayout extends TextInputLayout
{
public CustomTextInputLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public void setErrorEnabled(boolean enabled)
{
super.setErrorEnabled(enabled);
if (!enabled)
{
return;
}
try
{
TextView errorView = this.findViewById(R.id.textinput_error);
FrameLayout errorViewParent = (FrameLayout) errorView.getParent();
errorViewParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
errorView.setGravity(Gravity.CENTER); // replace CENTER with END or RIGHT
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
然后创建自定义TextInputLayout,如下所示:
<CustomTextInputLayout
android:id="@+id/til_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
app:layout_constraintBottom_toTopOf="@+id/til_last_name">
<EditText
android:id="@+id/et_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/first_name"
android:gravity="right"
/>
</CustomTextInputLayout>
恭喜你!你已经做了你所要求的 Kotlin中有一个不依赖于视图层次结构的漏洞:
class CustomTextInputLayout @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {
override fun setError(errorText: CharSequence?) {
super.setError(errorText)
with(findViewById<TextView>(R.id.textinput_error)) {
// this will work as long as errorView's layout width is
// MATCH_PARENT -- it is, at least now in material:1.2.0-alpha06
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_END
}
}
}
class CustomTextInputLayout@JVM重载构造函数(
context:context,attrs:AttributeSet?=null,defStyleAttr:Int=0
):TextInputLayout(上下文、属性、defStyleAttr){
覆盖fun setError(错误文本:CharSequence?){
super.setError(errorText)
带(findViewById(R.id.textinput\u错误)){
//只要errorView的布局宽度为
//MATCH_PARENT——至少在材质1.2.0-alpha06中是这样的
textAlignment=TextView.TEXT\u ALIGNMENT\u VIEW\u END
}
}
}
使用material componets版本1.2.0-rc01
,您可以创建从TextInputLayout继承的自定义TextInputLayout,将文本对齐方式更改为结尾(或您希望的位置),如下所示:
class CustomTextInputLayout : TextInputLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttrs: Int
) : super(context, attrs, defStyleAttrs)
override fun setErrorEnabled(enabled: Boolean) {
super.setErrorEnabled(enabled)
if (!enabled) {
return
}
try {
changeTextAlignment(
com.google.android.material.R.id.textinput_error,
View.TEXT_ALIGNMENT_VIEW_END
)
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun changeTextAlignment(textViewId: Int, alignment: Int) {
val textView = findViewById<TextView>(textViewId)
textView.textAlignment = alignment
}
}
基于@Emmanuel Guerra的回答。我也在helperText上应用了它:
class CustomTextInputLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = com.google.android.material.R.attr.textInputStyle
) : TextInputLayout(context, attrs, defStyleAttr) {
override fun setErrorEnabled(enabled: Boolean) {
super.setErrorEnabled(enabled)
if (!enabled) return
centerMiniText(com.google.android.material.R.id.textinput_error)
}
override fun setHelperTextEnabled(enabled: Boolean) {
super.setHelperTextEnabled(enabled)
if (!enabled) return
centerMiniText(com.google.android.material.R.id.textinput_helper_text)
}
private fun centerMiniText(textViewId: Int) {
try {
changeTextAlignment(textViewId, View.TEXT_ALIGNMENT_CENTER)
} catch (e: Exception) {
e.printStackTrace()
}
}
@Suppress("SameParameterValue")
private fun changeTextAlignment(textViewId: Int, alignment: Int) {
findViewById<TextView>(textViewId)?.textAlignment = alignment
}
}
class CustomTextInputLayout@JVM重载构造函数(
上下文:上下文,
属性集?=null,
defStyleAttr:Int=com.google.android.material.R.attr.textInputStyle
):TextInputLayout(上下文、属性、defStyleAttr){
override fun setErrorEnabled(已启用:布尔值){
super.setErrorEnabled(已启用)
如果(!启用)返回
centerMiniText(com.google.android.material.R.id.textinput\u错误)
}
覆盖fun SETHELPERTENDABLE(已启用:布尔值){
super.setHelperExtendabled(已启用)
如果(!启用)返回
centerMiniText(com.google.android.material.R.id.textinput\u helper\u text)
}
私人娱乐中心迷你文本(textViewId:Int){
试一试{
changeTextAlignment(textViewId、View.TEXT\u ALIGNMENT\u CENTER)
}捕获(e:例外){
e、 printStackTrace()
}
}
@抑制(“SameParameterValue”)
private fun changeTextAlignment(textViewId:Int,alignment:Int){
findViewById(textViewId)?.textAlignment=对齐
}
}
您可以尝试使用中的一个选项获取错误TextView
,然后调用setGravity(Gravity.RIGHT)
。我没有测试它,但是IIRC,错误TextView
有一个match\u parent
width,所以它应该按照您描述的对齐文本。很好的建议Mike M。但是我尝试了正确和结束,都没有用。必须使用下面详细介绍的编程方法…嗨,我正试图在edittext和右对齐的角上显示错误消息,你知道我是怎么做的吗?@ViVekH,你能解释一下你想要什么吗?这似乎不再有效(或者,在3.3.2/Android 9设置上肯定不行)。这对我不起作用。我已经在清单中禁用了它。有什么方法可以做到这一点吗?目前(2019-02-18)可以这样做,但当视图层次结构再次更改时,它将停止工作。这基本上是textview,其中显示textinputlayout的错误。我认为id应该是com.google.android.material.R.id.textinput\u error
,否则它会抱怨R没有找到我上面的工作代码。安卓工作室在我这边抱怨你的代码。除非你导入com.google.android.material.R
,你应该在回答中提到它。
void setErrorEnabled(boolean enabled) {
if (errorEnabled == enabled) {
return;
}
cancelCaptionAnimator();
if (enabled) {
errorView = new AppCompatTextView(context);
errorView.setId(R.id.textinput_error);
if (VERSION.SDK_INT >= 17) {
errorView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
//More code...
class CustomTextInputLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = com.google.android.material.R.attr.textInputStyle
) : TextInputLayout(context, attrs, defStyleAttr) {
override fun setErrorEnabled(enabled: Boolean) {
super.setErrorEnabled(enabled)
if (!enabled) return
centerMiniText(com.google.android.material.R.id.textinput_error)
}
override fun setHelperTextEnabled(enabled: Boolean) {
super.setHelperTextEnabled(enabled)
if (!enabled) return
centerMiniText(com.google.android.material.R.id.textinput_helper_text)
}
private fun centerMiniText(textViewId: Int) {
try {
changeTextAlignment(textViewId, View.TEXT_ALIGNMENT_CENTER)
} catch (e: Exception) {
e.printStackTrace()
}
}
@Suppress("SameParameterValue")
private fun changeTextAlignment(textViewId: Int, alignment: Int) {
findViewById<TextView>(textViewId)?.textAlignment = alignment
}
}