仅在提取UI模式下显示android:hint [英] Show android:hint only in Extract UI mode

查看:184
本文介绍了仅在提取UI模式下显示android:hint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android布局中有一个EditText列表.每个标签都标记有一个TextView,因此在纵向模式下不需要任何提示,甚至一个提示都是多余的.但是,在横向模式下,许多人将键盘配置为全屏显示并隐藏应用程序,直到隐藏键盘并将输入注入到选定的视图中为止.

I have a list of EditTexts in an android layout. Each one is labeled with a TextView, so no hint is necessary in portrait mode, and a hint would even be redundant. However, in landscape mode, many people have keyboards configured to take full screen and hide the app until the keyboard is hidden and the input is injected into the selected view:

如果您有一个用于输入的EditText字段,这可能很好,但是如果您有一个列表,则在输入之前没有人记住六个TextView标签.

This may be fine if you have one EditText field for input, but if you have a list, nobody is memorizing the six TextView labels before going through the inputs.

如何设置仅在键盘处于提取ui模式时才显示的提示-是横向还是纵向?

How can I set a hint that only appears if the keyboard is in extract ui mode - be it in landscape or portrait orientation?

推荐答案

我设法使用InputConnectionWrapper来解决此问题,InputConnectionWrapper具有用于进入全屏模式的显式回调.

I managed to solve this using InputConnectionWrapper, which has an explicit callback for entering fullscreen mode.

/**
 * [InputConnection] wrapper which applies hint text to 
 * the IME when entering fullscreen mode.
 */
class FullscreenHintInputConnection(
    delegate: InputConnection,
    private val editText: EditText,
    private val hintText: CharSequence
) : InputConnectionWrapper(delegate, false) {

    override fun reportFullscreenMode(enabled: Boolean): Boolean {
        if (enabled) {
            editText.hint = hintText
        } else {
            editText.hint = null
        }
        return super.reportFullscreenMode(enabled)
    }
}

在EditText子类中:

In an EditText subclass:

override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
    val connection = super.onCreateInputConnection(outAttrs)
    return FullscreenHintInputConnection(connection, this, "Lorem ipsum")
}

虽然此解决方案有效,但值得注意的是AppCompat可以更优雅地完成此操作.该库修改了传递给onCreateInputConnection的EditorInfo参数,从而完全避免了InputConnectionWrapper子类.

While this solution works, it's worth noting that AppCompat manages to do this a little more elegantly. The library modifies the EditorInfo argument passed into onCreateInputConnection, avoiding the InputConnectionWrapper subclass entirely.

AppCompatEditText.java

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return AppCompatHintHelper.onCreateInputConnection(
            super.onCreateInputConnection(outAttrs),
            outAttrs, 
            this
    );
}

这用于将从TextInputLayout提供的提示应用于基础TextInputEditText.其中一些API仅限于该库,因此您需要将它们复制到自己的项目中.但不幸的是,到目前为止,这项技术对我而言并不奏效,提示仍然是空白.

This is used to apply a hint supplied from a TextInputLayout to the underlying TextInputEditText. Some of these APIs are restricted to the library and therefore you would need to copy them into your own project. But unfortunately, this technique isn't working for me so far, the hint remains blank.

这篇关于仅在提取UI模式下显示android:hint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆