Edit text Password Toggle Android(编辑文本密码切换 Android)
问题描述
我试图在输入类型为文本密码的编辑文本中向用户显示输入的密码.
我在这样的切换图标上实现了手势监听器-
public boolean onTouch(View view, MotionEvent motionEvent) {开关(view.getId()){案例 R.id.ivPasswordToggle:开关(motionEvent.getAction()){案例 MotionEvent.ACTION_DOWN:Toast.makeText(getContext(),"show",Toast.LENGTH_SHORT).show();etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);休息;案例 MotionEvent.ACTION_UP:etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);Toast.makeText(getContext(),"hide",Toast.LENGTH_SHORT).show();休息;}休息;}返回真;}
我不知道是什么问题,任何帮助将不胜感激.
(针对AndroidX更新)
自支持库 v24.2.0 起.你可以很容易地做到这一点
你需要做的只是:
将设计库添加到您的依赖项中
依赖项 {实现com.google.android.material:material:1.2.1"}
将
结合使用TextInputEditText
与TextInputLayout
<com.google.android.material.textfield.TextInputLayoutxmlns:app=http://schemas.android.com/apk/res-auto"android:id=@+id/etPasswordLayout"android:layout_width=match_parent"android:layout_height="wrap_content";app:passwordToggleEnabled="true">
I am trying to show user the typed password in edit text whose input type is text Password.
I implemented gesturelistener over the toggle icon like this-
public boolean onTouch(View view, MotionEvent motionEvent) { switch (view.getId()) { case R.id.ivPasswordToggle: switch ( motionEvent.getAction() ) { case MotionEvent.ACTION_DOWN: Toast.makeText(getContext(),"show",Toast.LENGTH_SHORT).show(); etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); break; case MotionEvent.ACTION_UP: etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); Toast.makeText(getContext(),"hide",Toast.LENGTH_SHORT).show(); break; } break; } return true; }
i dont know what is wrong, any help will be appreciated.
解决方案(updated for AndroidX)
Since the Support Library v24.2.0. you can achivie this very easy
What you need to do is just:
Add the design library to your dependecies
dependencies { implementation "com.google.android.material:material:1.2.1" }
Use
TextInputEditText
in conjunction withTextInputLayout
<com.google.android.material.textfield.TextInputLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/etPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true"> <android.support.design.widget.TextInputEditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password_hint" android:inputType="textPassword"/> </com.google.android.material.textfield.TextInputLayout>
passwordToggleEnabled
attribute will make the password toggle appearIn your root layout don't forget to add
xmlns:app="http://schemas.android.com/apk/res-auto"
You can customize your password toggle by using:
app:passwordToggleDrawable
- Drawable to use as the password input visibility toggle icon.
app:passwordToggleTint
- Icon to use for the password input visibility toggle.
app:passwordToggleTintMode
- Blending mode used to apply the background tint.More details in TextInputLayout documentation.
这篇关于编辑文本密码切换 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:编辑文本密码切换 Android
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01