sin*_*enm 26 android autocomplete autocompletetextview android-edittext android-textinputlayout
我TextInputLayout在我的Android应用程序中使用,以便为我的输入字段实现整洁的浮动标签效果.我知道我也应该使用它TextInputEditText来允许在横向模式下显示提示并且输入填满整个屏幕.
但是,在我的一些输入字段中,我已经使用了自动完成AutoCompleteTextView(IMO的名称确实不一致 - "TextView"而不是"EditText" - 但这是另一个故事)并且显然是直接继承的EditText.因此它没有带来的功能TextInputEditText.
所以我想知道是否有办法实现相同的景观功能(没有自己的TextInputAutoCompleteTextView实现,也就是这样),并且还避免产生的lint警告.我在这里错过了什么吗?我想我得到的是他们没有EditText为这个特定事物制作所有直接和间接子类的自定义版本,所以我希望自己制作?
che*_*ork 31
有点晚了,但是,你必须推出自己的实现.好消息是,这是相当简单的.这是如何TextInputEditText实现的:
因此,这TextInputAutoCompleteTextView可能是什么样子.
public class TextInputAutoCompleteTextView extends AppCompatAutoCompleteTextView {
public TextInputAutoCompleteTextView(Context context) {
super(context);
}
public TextInputAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextInputAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
final InputConnection ic = super.onCreateInputConnection(outAttrs);
if (ic != null && outAttrs.hintText == null) {
// If we don't have a hint and our parent is a TextInputLayout, use it's hint for the
// EditorInfo. This allows us to display a hint in 'extract mode'.
final ViewParent parent = getParent();
if (parent instanceof TextInputLayout) {
outAttrs.hintText = ((TextInputLayout) parent).getHint();
}
}
return ic;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,借助AndroidX,您无需自定义某些内容。
只需添加材料组件样式(已添加1.1.0-alpha06,请参见发行说明)。
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Example TextInputLayout">
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
style="@style/Widget.MaterialComponents.AutoCompleteTextView.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
基于chessdork的回答,我想我会更详细地介绍如何将自动填充功能与项目提示结合起来.以下是我使用它的确切步骤:
1)确保你implementation 'com.android.support:design:26.1.0'的gradle依赖项.根据您的SDK版本,确切的包名称会略有不同.
2)TextInputAutoCompleteTextView从@ chessdork的答案中复制课程并将其放在项目中的公共课程中.
3)将自动填充edittext放在XML布局中的位置.它的结构应该是这样的:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp">
<mycompany.views.TextInputAutoCompleteTextView
android:id="@+id/myAutoFill"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/myHint"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
对于Material Components 库,只需使用TextInputLayout带有Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu样式的 。
就像是:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:hint="Hint..."
...>
<AutoCompleteTextView
android:background="@null"
.../>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7668 次 |
| 最近记录: |