Show android:hint only in Extract UI mode

落爺英雄遲暮 提交于 2019-12-08 17:31:53

问题


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:

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.

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


回答1:


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)
    }
}

In an EditText subclass:

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

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
    );
}

AppCompatHintHelper.java

class AppCompatHintHelper {
    static InputConnection onCreateInputConnection(InputConnection ic, EditorInfo outAttrs,
            View view) {
        if (ic != null && outAttrs.hintText == null) {
            ViewParent parent = view.getParent();
            while (parent instanceof View) {
                if (parent instanceof WithHint) {
                    outAttrs.hintText = ((WithHint) parent).getHint();
                    break;
                }
                parent = parent.getParent();
            }
        }
        return ic;
    }
}

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.



来源:https://stackoverflow.com/questions/38467803/show-androidhint-only-in-extract-ui-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!