Java 如何在Android上制作一个显示退格(?)字符的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24662898/
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-14 13:55:25  来源:igfitidea点击:

How to make a button that shows the backspace (?) character on Android?

javaandroidxmlspecial-charactersbackspace

提问by 9patchcoder

I'm trying to use the ? character as my backspace symbol in my android app. When I just copy and paste this character as the text value of my Button it works and shows the symbol in the simulator, but when I try to set this character dynamically in Java or when I try to use the Basic Latin value of it (\u232b) it just shows whitespace.

我正在尝试使用 ? 字符作为我的 android 应用程序中的退格符号。当我只是将此字符复制并粘贴为我的 Button 的文本值时,它可以工作并在模拟器中显示该符号,但是当我尝试在 Java 中动态设置此字符或尝试使用它的 Basic Latin 值时 ( \u232b)它只显示空白。

This is when I use the XML editor and my strings.xmlvalue:

这是我使用 XML 编辑器和我的strings.xml价值的时候:

enter image description here

在此处输入图片说明

My strings.xml:

我的strings.xml

<?xml version="1.0" encoding="utf-8"?>
  <resources>
      <string name="backSpace">?</string>
  </resources>   

In Java I tried hardcoding it like this, but they all result in whitespace:

在 Java 中,我尝试像这样对其进行硬编码,但它们都导致空格:

((Button) mView.findViewById(R.id.buttonClear)).setText("?");   
((Button) mView.findViewById(R.id.buttonClear)).setText("\u232b");` 
((Button) mView.findViewById(R.id.buttonClear)).setText('\u232b'+"");` 

采纳答案by 9patchcoder

Seems like the default Android font (Roboto / droid sans serif) doesn't include this character, so it can't display it (I still haven't figured out how the preview shows it). So you need to find a font that supports this character. The best candidate I've found is Arial Unicode MS, but these work too:

似乎默认的 Android 字体(Roboto / droid sans serif)不包含这个字符,所以它无法显示它(我仍然没有弄清楚预览是如何显示它的)。所以你需要找到一种支持这个字符的字体。我发现的最佳候选者是 Arial Unicode MS,但这些也有效:

  • Quivira (free)
  • Symbola
  • Segoe UI (windows phone's)
  • DejaVu sans (free)
  • Apple Symbols
  • 基维拉(免费)
  • 象征
  • Segoe UI(Windows 手机的)
  • DejaVu sans(免费)
  • 苹果符号

回答by Jon Skeet

That character is notU+0008. U+0008 is a control character, without a graphical representation.

那个字符不是U+0008。U+0008 是控制字符,没有图形表示。

? is U+232B(the "erase to the left" symbol), so if you use "\u232b"in your app it should be fine.

? 是U+232B(“向左擦除”符号),所以如果你"\u232b"在你的应用中使用它应该没问题。

回答by Dmitry

For what's it worth, they offer a standard icon that represents this symbol. It is part of the 'Action Bar Icon Pack' form here. It's in this folder:

对于它的价值,他们提供了一个代表这个符号的标准图标。它是此处“操作栏图标包”表单的一部分。它在这个文件夹中:

\Android Design - Icons 20131120\Action Bar Icons\holo_light_content_backspace

enter image description here

在此处输入图片说明

回答by Trevor

My approach is to use an ImageButtontogether with standard platform Drawables. You can actually see the standard Drawables available for various platforms by browsing your Android SDK directory: Sdk/platforms/android-XXX/data/res/

我的方法是ImageButton与标准平台 Drawables 一起使用。通过浏览您的 Android SDK 目录,您实际上可以看到适用于各种平台的标准 Drawable:Sdk/platforms/android-XXX/data/res/

This gives you a button with the backspace symbol:

这为您提供了一个带有退格符号的按钮:

    <ImageButton
        android:src="@drawable/sym_keyboard_return"
        ...
    />

Note: Google actually advise against referencing Android resources directly and advise making a local copy (see here). Therefore, try the above to see what the icon looks like (or have a browse inside the SDK folders mentioned above to see all the .png drawables directly), but for production it is best to copy the .png images for each desired resolution to your own project and reference those.

注意:Google 实际上建议不要直接引用 Android 资源并建议制作本地副本(请参阅此处)。因此,请尝试以上操作以查看图标的外观(或浏览上述 SDK 文件夹以直接查看所有 .png 可绘制对象),但对于生产,最好将每个所需分辨率的 .png 图像复制到你自己的项目并参考那些。

For what it's worth, there are various other very useful symbol images, such as a 'return' symbol (sym_keyboard_return.png, for example). Many of them such as sym_keyboard_return aren't referenced in android.R anyhow for some reason, so you certainly have to copy that particular one to your project.

对于它的价值,还有各种其他非常有用的符号图像,例如“返回”符号(例如 sym_keyboard_return.png)。由于某种原因,其中许多(例如 sym_keyboard_return )在 android.R 中无论如何都没有被引用,因此您当然必须将该特定的那个复制到您的项目中。

回答by Mridul Kumar Sharmah

Example:1

示例:1

If you want the action of this character of erasing, use this--

如果你想要擦除这个字符的动作,就用这个——

<Key android:codes="-5" android:keyLabel="?"
            android:keyWidth="15%p" android:keyEdgeFlags="right"
           android:isRepeatable="true"/>

Example:2

示例:2

If you just want to display the character and don't need the action of the character of erasing, use this--

如果只想显示字符,不需要擦除字符的动作,就用这个——

<Key android:codes="0x232B" android:keyLabel="?"
        android:keyWidth="15%p" android:keyEdgeFlags="right"
        android:isRepeatable="true"/>