在 TextInputLayout 中有一个用于 InputType textPassword 的密码可见性切换按钮。
是否有可能以某种方式捕获切换事件?
我找不到任何公共方法
我有一个 TextInputLayout 作为密码。我添加了 passwordToggleEnabled=true 来切换密码可见性。当用户切换密码可见性时,我需要捕获事件。我怎样才能做到这一点。
<android.support.design.widget.TextInputLayout
android:id="@+id/password_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_new_password"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个登录页面,这是我TextInputLayout的密码字段。
<android.support.design.widget.TextInputLayout
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:hint="Password"
app:passwordToggleDrawable="@drawable/eye_outline"
android:theme="@style/GFSTextInputLayoutTheme"
app:passwordToggleEnabled="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textColor="@color/black"
android:textColorHint="@color/light_grey"
android:textSize="20dp" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
但不知何故,app:passwordToggleEnabled="true"似乎不起作用。
这是我的 gradle 导入:
compile 'com.android.support:design:27.1.1'
compile 'com.android.support:support-v4:27.1.1'
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.android.support:recyclerview-v7:27.1.1'
compile 'com.android.support:support-vector-drawable:27.1.1'
compile 'com.android.support.constraint:constraint-layout:1.1.0'
compile 'com.android.support:recyclerview-v7:27.1.1'
compile 'com.android.support:cardview-v7:27.1.1'
Run Code Online (Sandbox Code Playgroud)
编辑:
样式文件
<style name="GFSTextInputLayoutTheme" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@color/light_grey</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@color/light_grey</item>
<item name="colorControlNormal">@color/light_grey</item>
<item …Run Code Online (Sandbox Code Playgroud) java passwords android android-textinputlayout textinputlayout
以下代码与轮廓输入框完美配合。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/notes"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/text_notes"
app:boxBackgroundColor="@color/colorWhite">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_notes"
android:fontFamily="sans-serif-black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我在整个应用程序中设置了自定义字体。按以下代码设置后,它崩溃了。以下样式用于应用程序主题。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textViewStyle">@style/SansSerifTextViewStyle</item>// Custom font style
<item name="buttonStyle">@style/SanSerifButtonStyle</item> // Custom font style
</style>
Run Code Online (Sandbox Code Playgroud)
当我删除以下行时,应用程序不会崩溃。但是勾勒出来的框 UI 不会出现。尝试通过 Java 代码设置,无效。
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
Run Code Online (Sandbox Code Playgroud) 我在“NullPointerException: Attempt to invoke virtual method 'void android.view.View.getBoundsOnScreen(android.graphics.Rect)' on a null object reference”崩溃日志中发现崩溃,我验证了一些关于这次崩溃的stackoverflow建议。我发现下面的链接建议在 TextInputLayout 中使用提示而不是 TextInputEditText,所以我想知道是什么让它与众不同?
下面的链接显示在TextInputLayout使用提示: SO 谷歌显示在TextInputEditText使用提示: Google_Recommendation
我在 TextInputLayout 和 TextInputEditText 的两个地方都尝试了提示。提示同样在两个地方都运行良好。
我想知道这两种方法有什么区别,哪一种更适合使用。
我想以编程方式更改 TextInputLayout 的轮廓,但我似乎无法让它工作。有一个选项可以通过 XML 完成(其他 SO 用户使用 XML 提出的问题),但这对我来说无法使用,因为我需要动态着色。我目前有以下布局:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="@+id/color_outline"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Choose color"/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我试图通过查看 TextInputLayout 的各种框方法来应用着色,但它没有任何效果。
internal fun String.toIntColor() = Integer.parseInt(this.replaceFirst("#", ""), 16)
val colorOutline: TextInputLayout = view.findViewById(R.id.color_outline)
colorOutline.boxStrokeColor = "#006699".toIntColor()
Run Code Online (Sandbox Code Playgroud)
如何动态着色,如下图所示?
android android-edittext android-textinputlayout material-components material-components-android
迁移到最新版本的 Material Components ( 1.1.0-alpha09) 后,我注意到TextInputLayout有一些填充水平填充。我的目标是实现没有背景或轮廓框的旧样式文本字段。所以我扩展了其中一种样式,即Widget.MaterialComponents.TextInputLayout.FilledBox并将背景颜色设置为透明。
<style name="Widget.MyApp.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
<item name="boxBackgroundColor">@color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
现在的问题是,这个输入字段在我的应用程序中看起来很奇怪,具有这种水平填充(对于填充框或轮廓框是必需的)。
所以我的问题是,我怎样才能删除这个填充?
android material-design android-textinputlayout material-components material-components-android
我正在使用 Material 设计库制作圆形的 editText,我能够做到这一点,但现在我希望它看起来更小,我使用了密集的 textField 样式,但我仍然希望视图的高度更小比起那个来说。问题是浮动 Label 标签,我没有为 textField 设置提示,但 Label 标签仍然占用一些空白空间。
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:endIconMode="custom"
app:endIconDrawable="@drawable/ic_speaker_24dp"
app:layout_constraintBottom_toBottomOf="@+id/topBar_view"
app:layout_constraintEnd_toEndOf="@+id/topBar_view"
app:layout_constraintStart_toEndOf="@+id/separater_line"
app:layout_constraintTop_toTopOf="parent"
app:boxCornerRadiusTopStart="20dp"
app:boxCornerRadiusTopEnd="20dp"
app:boxCornerRadiusBottomStart="20dp"
app:boxCornerRadiusBottomEnd="20dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我只希望 textField 看起来像 chrome url 搜索栏,苗条,键入时提示消失,而不是浮动标签。
编辑:我尝试了该app:hintEnabled="false"属性,但仍有一个空白空间
android material-design android-textinputlayout material-components material-components-android
如果我使用最新的材质库版本com.google.android.material:material:1.2.0-alpha02,我在 TextInputEditText 和他的提示消息方面遇到了一些麻烦。我设置了 5 行可滚动 textInputEditText 并且我想显示提示消息与该文本输入的上边距对齐。在布局编辑器上出现在正确的位置,但是当我运行应用程序时,位置是垂直居中的。为什么 ?
相反,我没有遇到这个问题com.google.android.material:material:1.1.0-beta02
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/sendMailContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/popupTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="@string/sendBusinessCard"
android:textColor="@android:color/black"
android:textSize="22sp" />
<TextView
android:id="@+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sendBusinessCard"
android:textColor="@android:color/black"
android:textSize="18sp"
android:layout_below="@+id/popupTitle"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/noteMessageContainer"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/subTitle"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/noteMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/emailText"
android:inputType="text|textMultiLine|textCapSentences"
android:textColor="@android:color/black"
android:textColorHint="@color/gray9B"
android:textSize="16sp"
android:isScrollContainer="true"
android:scrollbars="vertical"
android:textIsSelectable="true"
android:minLines="5"
android:maxLines="5"
android:gravity="top"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/notePopupLeftButton"
android:layout_width="180dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/noteMessageContainer"
android:layout_marginBottom="20dp"
android:layout_marginStart="20dp" …Run Code Online (Sandbox Code Playgroud) android android-layout android-textinputlayout android-textinputedittext material-components-android
我知道可以单击这样的视图:
view.PerformClick()
Run Code Online (Sandbox Code Playgroud)
我如何在TextInputLayoutEndIcon上做到这一点button?
更新
问题是我有一堆 InputLayouts 并使用通用函数在它们上设置点击侦听器,就像这样
fun setTextInputLayoutListeners(
inputLayout: TextInputLayout, editText: TextInputEditText,
actionSet: () -> Unit,
actionClear: () -> Unit
) {
with (inputLayout) {
setOnClickListener { actionSet() }
setEndIconOnClickListener { actionClear() }
}
editText.setOnClickListener { actionSet() }
}
Run Code Online (Sandbox Code Playgroud)
并像这样用不同的参数调用它
setTextInputLayoutListeners(
categoryInputLayout, categoryEditText, { onCategoryClick() }, { onCategoryClear() }
)
setTextInputLayoutListeners(
dateInputLayout, dateEditText, { onDateClick() }, { onDateClear(calendar) }
)
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找一个通用的解决方案,有点
inputLayout.EndIcon.PerformClick()
Run Code Online (Sandbox Code Playgroud)