adb shell 输入带空格的文本

15

如何使用adb shell input text发送像"some text"这样有空格的文本?

找到以下解决方案

adb shell input text "some%stext"可以正常工作。 但是否有更简单的方法将空格替换为%s?

示例:

$ adb shell input text "some text"
Error: Invalid arguments for command: text
Usage: input [<source>] <command> [<arg>...]

The sources are: 
      keyboard
      mouse
      joystick
      touchnavigation
      touchpad
      trackball
      dpad
      stylus
      gamepad
      touchscreen

The commands and default sources are:
      text <string> (Default: touchscreen)
      keyevent [--longpress] <key code number or name> ... (Default: keyboard)
      tap <x> <y> (Default: touchscreen)
      swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      press (Default: trackball)
      roll <dx> <dy> (Default: trackball)
6个回答

23

您可以使用\<space>来实现,就像这样:

adb shell input text "some\ text"

可以运行。adb 1.0.36。来自G的Android手机Pixel 2。 - Roy

8

如果安装了gnu-linux sed(或其他软件)(大多数Linux机器都预装了),则可以使用sed将空格替换为%s。

adb shell input text $(echo "some text with spaces" | sed 's/ /\%s/g')

百分号必须用 \ 进行转义。


Sed 也可在 Android 上大部分 busybox 实现中使用,或在 root 后添加。由于您正在使用 adb shell input,因此 $() 将在本地解释,而不是在 Android 系统中(即 adb shell)使用,所以您将使用系统中的 sed。精简的最小工具箱不包含 sed。 - cde
问题要求使用adb shell input,而不是在adb shell内进行文本操作^^ - kai-dj
这是什么编码类型?冒号(:)对于传递URL有什么用处? - RzR

2
您可以像这样发送您的文本:

Text = "some text with spaces"

用%s替换空白

adb shell input text some%stext%swith%sspaces

这实际上并没有回答问题的关键。OP 知道你需要 %s,他们不想手动编辑所有空格。 - cde
@cde 当然,我假设使用字符串替换函数将空格更改为%s。 - Ingo

1

adb shell "input text '一些文本'"


0
将文本用单引号括起来:
adb shell input text 'some text'

0

用最佳方式将特殊字符替换为 .

 val message = text.replace("|", "\\|")
            .replace("\"", "\\\"")
            .replace("\'", "\\\'")
            .replace("<", "\\<")
            .replace(">", "\\>")
            .replace(";", "\\;")
            .replace("?", "\\?")
            .replace("`", "\\`")
            .replace("&", "\\&")
            .replace("*", "\\*")
            .replace("(", "\\(")
            .replace(")", "\\)")
            .replace("~", "\\~")
            .replace(" ", "\\ ")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接