Android 如何在 string.xml 文件中放置“-”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10578551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:13:38  来源:igfitidea点击:

How to put a "-" in string.xml file

androidstring

提问by roiberg

I need to be able to put a "-" in the string inside my strings.xmlfile.

我需要能够在我的strings.xml文件中的字符串中放置一个“-” 。

My problem is that when I am putting my string which is "1261eba2-9d8c-11e1-93e3-40409e0f44a1", eclipse yells:

我的问题是,当我放置字符串时"1261eba2-9d8c-11e1-93e3-40409e0f44a1",eclipse 会大喊:

Multiple annotations found at this line: - Replace "-" with an "en dash" character (–, &;#8211;)

在这一行找到多个注释: - 将“-”替换为“破折号”字符(–, &;#8211;)

How can I fix this?

我怎样才能解决这个问题?

回答by Phil

So, when you read the error message, your answer will be that you have to replace -with –. Then it should work fine =)

因此,当您阅读错误消息时,您的答案将是您必须替换-–. 那么它应该可以正常工作 =)

http://en.wikipedia.org/wiki/Dash

http://en.wikipedia.org/wiki/Dash

回答by Tal Weiss

The other answers are OK for when you want to display the string to the user. The user can't really tell the difference between a "real" dash and the unicode trickery.
But, if you really must have the dash (e.g. because that string is used as a password somewhere, or as a url key for an API) then you can just use this format:

当您想向用户显示字符串时,其他答案都可以。用户无法真正分辨出“真正的”破折号和 unicode 诡计之间的区别。
但是,如果您真的必须使用破折号(例如,因为该字符串在某处用作密码,或用作 API 的 url 密钥),那么您可以使用以下格式:

<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
    <string name="EVA_API_KEY">3c42b725-5e20-41c8-982f-dee40be8a05b</string>
</resources>

The warning will be removed and the string can be read using the regular:

警告将被删除,并且可以使用常规读取字符串:

getResources().getString(R.string.EVA_API_KEY);

回答by RajaReddy PolamReddy

Use back slash( \ ) in front of every special character. like me\&android.

在每个特殊字符前使用反斜杠(\)。像我一样\&android。

This called escape character. ( \ )

这称为转义字符。( \ )

回答by Amr Ashraf

The dash is a punctuation mark that is similar to a hyphen or minus sign, but differs from both of these symbols primarily in length and function. The most common versions of the dash are the en dash (–) and the em dash (—), named for the length of a typeface's lower-case n and upper-case M respectively.

破折号是一个标点符号,类似于连字符或减号,但与这两个符号的主要区别在于长度和功能。破折号最常见的版本是短破折号 (–) 和长破折号 (-),分别以字体小写 n 和大写 M 的长度命名。

Reference

参考

Just replace -with because when you type a dash on the keyboard, XML reads dash as minus, that's all.

只需替换为-因为当您在键盘上键入破折号时,XML 将破折号读取为减号,仅此而已。

回答by Ganesh Katikar

For hyphen use (&#45) (-)...

对于连字符使用 (-) (-)...

<string name="abc">Welcome &#45; Bro...</string>

and For more symbol use below link

和以下链接的更多符号使用

http://www.degraeve.com/reference/specialcharacters.php

http://www.degraeve.com/reference/specialcharacters.php

Enjoy...

享受...

回答by Maz

You probably have this:

你可能有这个:

<string name="test1">1261eba2-9d8c-11e1-93e3-40409e0f44a1</string>

But you need either one of these:

但是您需要以下任何一种:

<string name="test2">1261eba2&#8211;9d8c&#8211;11e1&#8211;93e3&#8211;40409e0f44a1</string>
<string name="test3">1261eba2–9d8c–11e1–93e3–40409e0f44a1</string>

In the second one the - is replaced by a –. It's hard to tell the difference visually.

在第二个中 - 替换为 -。很难从视觉上分辨出差异。

回答by Hasan El-Hefnawy

The quick fix shortcut in Eclipse is Ctrl+ 1by default and in Android Studio is Alt+ Enterby default.

Eclipse 中的快速修复快捷方式默认为Ctrl+ 1,Android Studio 中默认为Alt+ Enter