【问题标题】:adb shell input text with spaceadb shell 输入带空格的文本
【发布时间】:2022-05-06 19:04:26
【问题描述】:

如何使用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)

【问题讨论】:

    标签: android shell adb


    【解决方案1】:

    您可以使用\&lt;space&gt; 进行操作,如下所示:

    adb shell input text "some\ text"
    

    【讨论】:

    • 为我工作。亚行 1.0.36。 G 的 Android 手机 Pixel 2。
    【解决方案2】:

    安装了 gnu-linux sed(或其他)(大多数 linux 机器都预装了它) - 您可以使用 sed 将空格替换为 %s。

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

    % 符号必须用 \ 转义。

    【讨论】:

    • Sed 也可用于在 android 上找到的大多数繁忙的盒子实现中或在生根后添加。由于您使用的是adb shell input,因此 $() 在本地解释,而不是在 android 系统内部(又名adb shell),您使用的是系统的 sed。精简后的最小工具箱没有 sed。
    • Q 要求使用 adb shell 输入,而不是用于 adb shell 内的文本操作 ^^
    • 这是什么编码类型? ':' 对传递 URL 有用吗?
    【解决方案3】:

    您可以发送如下文本:

    Text = "some text with spaces"
    

    用 %s 替换空格

    adb shell input text some%stext%swith%sspaces
    

    【讨论】:

    • 这实际上并没有回答 op 的问题。 Op 知道您需要 %s,他们不想手动编辑所有空格。
    • @cde 我当然假设将使用字符串替换函数将空格更改为 %s
    【解决方案4】:

    adb shell "输入文本'一些文本'"

    【讨论】:

      【解决方案5】:

      用.替换特殊字符的最佳方法。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 2015-02-12
        • 1970-01-01
        相关资源
        最近更新 更多