简体   繁体   中英

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

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 ( \⌫ ) it just shows whitespace.

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

在此输入图像描述

My 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:

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

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

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

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:

  • Quivira (free)
  • Symbola
  • Segoe UI (windows phone's)
  • DejaVu sans (free)
  • Apple Symbols

My approach is to use an ImageButton together 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/

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.

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.

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\05_content_backspace

在此输入图像描述

Example: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

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM