TextInputLayout 中的提示与 TextInputEditText 中的提示有什么区别

What is difference between hint in TextInputLayout vs hint in TextInputEditText

我在 "NullPointerException: Attempt to invoke virtual method 'void android.view.View.getBoundsOnScreen(android.graphics.Rect)' on a null object reference" 的崩溃日志中发现了崩溃,并且我验证了针对此崩溃的一些 Whosebug 建议。我发现下面 link 建议在 TextInputLayout 中使用提示而不是 TextInputEditText,所以我想知道它有什么不同?

下面link建议在TextInputLayout中使用提示: SO Google 显示在 TextInputEditText 中使用提示: Google_Recommendation

我在 TextInputLayout 和 TextInputEditText 两个地方都尝试了提示。提示在两个地方都同样有效。

我想知道这两种方法有什么区别,哪种方法更适合使用。

在设计支持库中引入 TextInputLayout 以在 EditTextTextInputEditText 上显示浮动标签。 EditTextTextInputEditText 必须由 TextInputLayout 包裹起来才能显示浮动标签。

经验法则TextInputLayout 应该换行 TextInputEditText

你应该把提示放在哪里

根据官方documentation

The hint should be set on the TextInputLayout, rather than the EditText. If a hint is specified on the child EditText in XML, the TextInputLayout might still work correctly; TextInputLayout will use the EditText's hint as its floating label. However, future calls to modify the hint will not update TextInputLayout's hint. To avoid unintended behavior, call setHint(CharSequence) and getHint() on TextInputLayout, instead of on EditText.

注意 TextInputEditTextEditText 的特殊 sub-class,设计用作 TextInputLayout 的子代。

希望对您有所帮助

正在回答,因为我还不能发表评论。

就我而言,我只从华为设备收到您提到的崩溃。 一旦我将所有提示移至应用程序中的 TextInputLayouts,所有崩溃都停止了。

我的理解是华为构建的 android 版本缺少空检查导致崩溃。

我现在总是将我的提示放在 TextInputLayout 上。