首页 > 系统 > Android > 正文

Android Material Design -TextInputLayout

2019-11-06 09:56:16
字体:
来源:转载
供稿:网友

Android Material Design 有一个重要的控件 —-TextInputLayout

我先上一下TextInputLayout的布局东西

效果图

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.king_mi.lottery.MainActivity"> <TextView android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="还未开奖" android:textSize="30sp" android:layout_margin="20dp" android:textColor="@color/colorAccent" /> <android.support.design.widget.TextInputLayout android:id="@+id/til_value_a" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="15" android:hint="请输入数值A" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/til_value_b" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="15" android:hint="请输入数值B" > <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/til_value_times" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="15" android:hint="请输入参数人次" > <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btn_ok" android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取开奖结果" /></LinearLayout>

上面可以看到我用了TextInputLayout的东西 1. TextInputLayout 和Scrollview一样,只能包含一个子控件 2. TextInputLayout 中只能包含TextInputEditText或EditText,他们的效果是一样的 3. TextInputLayout 中cunterMaxLength 是提示可以输入最大的次数,仅提示作用. 4. TextInputLayout 中hint 可以在布局文件与代码中设置,但不要同时设置,以防重叠.建议直接在布局文件里面设置好.

@OnClick(R.id.btn_ok) public void onClick() { String valueA = tilValueA.getEditText().getText().toString().trim(); String valueB = tilValueB.getEditText().getText().toString().trim(); String valueTimes = tilValueTimes.getEditText().getText().toString().trim(); if (doCheck(valueA, valueB, valueTimes)) { getResult(valueA, valueB, valueTimes); } }这里注意获取Edittext中的值,我们不需要去声明EditText控件.

重点来了

PRivate boolean doCheck(String valueA, String valueB, String valueTimes) { boolean check = true; // if (TextUtils.isEmpty(valueA)) { tilValueA.setError("请输入数值A"); tilValueA.setErrorEnabled(true); check = false; } else { tilValueA.setErrorEnabled(false); } if (TextUtils.isEmpty(valueB)) { tilValueB.setError("请输入数值B"); check = false; } else { tilValueB.setError(null); } if (TextUtils.isEmpty(valueTimes)) { tilValueTimes.setError("请输入参与人次!"); check = false; } else { tilValueTimes.setError(null); } return check; }setError(“XXX”) 可以在输入控件的下方做一个错误的提示,用setError(null) 来清除这个提示. setErrorEnable(true)与setErrorEnable(false)试了多次没有发现有啥作用,如果说有作用,就是setErrorEnable(false)和setError(null)一样可以清除错误标记个人建议只需要使用setError就可以了,setErrorEnable没有啥作用.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表