zoukankan      html  css  js  c++  java
  • android 代码edittext删除或者替换光标处的字串

    https://stackoverflow.com/questions/3609174/android-insert-text-into-edittext-at-current-position

    Cpt.Ohlund gave me the right hint. I solved it, now, partly with using EditText.getSelectionStart(), but I realized that you can also replace the selected text with the same expression and you don't need String.subString() for that.

    int start = Math.max(myEditText.getSelectionStart(), 0);
    int end = Math.max(myEditText.getSelectionEnd(), 0);
    myEditText.getText().replace(Math.min(start, end), Math.max(start, end),
            textToInsert, 0, textToInsert.length());

    This works for both, inserting a text at the current position and replacing whatever text is selected by the user. The Math.max() is necessary in the first and second line because, if there is no selection or cursor in the EditText, getSelectionStart() and getSelectionEnd() will both return -1. The Math.min() and Math.max() in the third line is necessary because the user could have selected the text backwards and thus start would have a higher value than end which is not allowed for Editable.replace().

  • 相关阅读:
    aiohttp简介及快速使用
    Git的学习与使用
    基于scrapy-redis的分布式爬虫
    异步编程之asyncio简单介绍
    Scrapy框架中的CrawlSpider
    scrapy中selenium的应用
    ua池和代理池
    Scrapy持久化存储
    Scrapy的日志等级和请求传参
    virtualenv搭建Python虚拟环境
  • 原文地址:https://www.cnblogs.com/welhzh/p/8661747.html
Copyright © 2011-2022 走看看