【问题标题】:EditText with a hint on multiple lines带有多行提示的 EditText
【发布时间】:2011-05-23 15:22:14
【问题描述】:

我有一个大的EditText 盒子。我希望框中的提示看起来像这样(不打算突出语法):

Hint about the first line
Hint about the second line
Hint about the third line

我尝试在 EditText 上使用 android:hint="@string/hints",并在 strings.xml 中使用以下内容,但提示仍在单行上。

<string name="hints">
Hint about the first line
Hint about the second line
Hint about the third line
</string>

如何在EditText 中获得多行提示?

【问题讨论】:

标签: android android-edittext


【解决方案1】:

为每一行制作一个单独的字符串资源:

<string name="hint_one">Hint about the first line</string>
<string name="hint_two">Hint about the second line</string>
<string name="hint_three">Hint about the third line</string>

不要在布局 XML 中包含 android:hint 属性。在创建EditText 的地方手动设置提示:

// get the string values
final Resources res = getResources();
final String one   = res.getString(R.string.hint_one);
final String two   = res.getString(R.string.hint_two);
final String three = res.getString(R.string.hint_three);

// set the hint
final EditText et = (EditText) findViewById(R.id.some_edit_text);
et.setHint( one + "\n" + two + "\n" + three );

【讨论】:

    【解决方案2】:

    您可以通过显式指定在字符串中包含换行符:

    <string name="hint">Manny, Moe and Jack\nThey know what I\'m after.</string>
    

    XML 中的换行符被视为空格。

    【讨论】:

    • 我为提示新行尝试了相同的 \n,它在设计时工作我的意思是在 XML 中,但它在真实设备中的真实设备中不起作用,它的显示单line.可以解释一下是什么问题?
    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 2018-05-04
    • 1970-01-01
    • 2014-11-09
    • 2012-05-20
    • 2011-06-19
    • 1970-01-01
    • 2011-02-28
    相关资源
    最近更新 更多