Gob*_*ber 10 android android-textinputlayout
我正在使用TextInputLayout和支持库中的新函数:passwordToggleEnabled.这给了一个漂亮的"眼睛" - 图标,让用户可以打开和关闭密码可见性.
我的问题是,是否有办法使用此功能,但开始密码可见?
我的xml:
<android.support.design.widget.TextInputLayout
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/password_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我还没有找到一种方法在xml中执行此操作,而不是在呈现视图后手动切换可见性的方法.如果我将EditText的输入类型设置为textVisiblePassword,则不会显示切换.如果我在代码中使用例如mPasswordEditText.setTransformationMethod(null); 显示密码但切换消失,用户无法再次隐藏密码.我知道我可以手动完成所有操作,但只是想知道我是否可以使用新的魔术切换工作
最简单的方法在下面另一个解决方案是这个答案的最后
private void setupPasswordToggleView() {
final TextInputLayout textInputLayout = mRootView.findViewById(R.id.password);
// You can skip post-call and write directly the code which is inside run method.
// But to be safe (as toggle-view is child of TextInputLayout, post call
// has been added.
textInputLayout.post(new Runnable() {
@Override
public void run() {
CheckableImageButton passwordToggleView = textInputLayout.findViewById(R.id.text_input_password_toggle);
// passwordToggleView.toggle(); // Can not use as restricted to use same library group
// passwordToggleView.setChecked(true); // Can not use as restricted to use same library group
passwordToggleView.performClick();
}
});
}
Run Code Online (Sandbox Code Playgroud)
现在让我解释一下答案
在查看TextInputLayout.java我发现的代码时,有一个布局design_text_input_password_icon.xml被添加到TextInputLayout.java. 下面是那个代码
private void updatePasswordToggleView() {
if (mEditText == null) {
// If there is no EditText, there is nothing to update
return;
}
if (shouldShowPasswordIcon()) {
if (mPasswordToggleView == null) {
mPasswordToggleView = (CheckableImageButton) LayoutInflater.from(getContext())
.inflate(R.layout.design_text_input_password_icon, mInputFrame, false);
mPasswordToggleView.setImageDrawable(mPasswordToggleDrawable);
mPasswordToggleView.setContentDescription(mPasswordToggleContentDesc);
mInputFrame.addView(mPasswordToggleView); // << HERE IS THAT
.........
}
Run Code Online (Sandbox Code Playgroud)
现在下一个目标是查找design_text_input_password_icon.xml和查找切换视图的 id。所以在这里找到了布局design_text_input_password_icon.xml,它写成
18<android.support.design.widget.CheckableImageButton
19 xmlns:android="http://schemas.android.com/apk/res/android"
20 android:id="@+id/text_input_password_toggle"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:layout_gravity="center_vertical|end|right"
24 android:background="?attr/selectableItemBackgroundBorderless"
25 android:minHeight="48dp"
26 android:minWidth="48dp"/>
Run Code Online (Sandbox Code Playgroud)
我找到了text_input_password_toggle该视图的 id ,现在一切都很容易,只需在它的视图组中找到该视图并对其执行操作即可。
另一种解决方案是迭代子项TextInputLayout并检查它是否是CheckableImageButton,然后对其进行单击。这样就不会依赖该视图的 id,如果 Android 更改视图的 id,我们的解决方案仍然有效。(尽管在正常情况下它们不会更改视图的 ID)。
private void setupPasswordToggleViewMethod2() {
final TextInputLayout textInputLayout = mRootView.findViewById(R.id.password);
textInputLayout.post(new Runnable() {
@Override
public void run() {
View toggleView = findViewByClassReference(textInputLayout, CheckableImageButton.class);
if (toggleView != null) {
toggleView.performClick();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
当findViewByClassReference(View rootView, Class<T> clazz) 原始工具类的定义如下
public static <T extends View> T findViewByClassReference(View rootView, Class<T> clazz) {
if(clazz.isInstance(rootView)) {
return clazz.cast(rootView);
}
if(rootView instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) rootView;
for(int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
T match = findViewByClassReference(child, clazz);
if(match != null) {
return match;
}
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6316 次 |
| 最近记录: |