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().
android 代码edittext删除或者替换光标处的字串的更多相关文章
- Android中EditText样式修改 聚焦光标、背景
在Android开发中,根据项目的需求,需要定制一些特殊的样式,例如:使用EditText时,聚焦时的背景及光标图片使用自定义而非android系统默认的.这两天,在项目中涉及此需求,现记录如下: 首 ...
- vim复制,粘贴,删除,撤销,替换,光标移动等用法
我接触linux的时候,用的是nano这个文本编辑器,个人觉得他的最大优点就是方便,快速.打开文件,修改文件,到退出都十分的快速,简单.这一点上比vi,vim要强,但是nano不能显示彩色字,不能进行 ...
- Android零基础入门第87节:Fragment添加、删除、替换
前面一起学习了Fragment的创建和加载,以及其生命周期方法,那么接下来进一步来学习Fragment的具体使用,本期先来学习Fragment添加.删除.替换. 一.概述 在前面的学习中,特别是动态加 ...
- 35.Android之带删除按钮EditText学习
今天实现Android里自定义带删除功能的EditText,效果如下: 当输入内容时,EditText变为带有一个删除功能按钮的编辑框,如图: 实现代码很简单,直接上代码, 布局文件xml: < ...
- [Android教程]EditText设置/隐藏光标位置、选中文本和获取/清除焦点
有时候需要让光标显示在EditText的指定位置或者选中某些文本.同样,为了方便用户输入以提升用户体验,可能需要使EditText获得或失去焦点. 1. 设置光标到指定位置 EditText et ...
- android使用mount挂载/system/app为读写权限,删除或替换系统应用
注意:以下代码中#开头的则为需要执行的shell命令,其他的为打印的结果.#代表需要使用ROOT权限(su)执行,所以想要修改您android手机某个目录挂载为读写,首先需要有ROOT权限! 先要得到 ...
- Android 设置EditText光标位置(转)
Android 设置EditText光标位置 最后 CharSequence text = edtTxt_my_account_edit_nickname.getText();if (text ins ...
- Android中Edittext的属性
//此为转载别人的,挺不错的 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" ...
- Android 禁止Edittext弹出系统软键盘 的几种方法
第一种方法:在XML文件下添加: android:focusable="true" android:focusableInTouchMode="true" 第二 ...
随机推荐
- 【cs229-Lecture5】生成学习算法:1)高斯判别分析(GDA);2)朴素贝叶斯(NB)
参考: cs229讲义 机器学习(一):生成学习算法Generative Learning algorithms:http://www.cnblogs.com/zjgtan/archive/2013/ ...
- Word 2010 插入其他文件的方法
1. 2.
- laravel部署常用命令
php composer install composer dump-autoload php artisan key:generate .env 及 config/database.php里的数据库 ...
- C# 输出带颜色文字,用于实时日志输出
private void button1_Click(object sender, EventArgs e) { LogMessage("绿色"); 4 LogError(&quo ...
- HDU 1455 Sticks(经典剪枝)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- linux常用命令之scp详解
使用scp的前提: 1.服务端启动了sshd服务 2.是本地和远程两端的系统都必须要有scp这个命令.即openssh-clients软件包 [安装方法] [root@ ~]# yum install ...
- Android 使用MediaPlayer 播放 视频
http://pan.baidu.com/s/1lgKLS package cn.bgxt.surfaceviewdemo; import java.io.File; import android.m ...
- c++的矩阵乘法加速trick
最近读RNNLM的源代码,发现其实现矩阵乘法时使用了一个trick,这里描述一下这个trick. 首先是正常版的矩阵乘法(其实是矩阵乘向量) void matrixXvector(float* des ...
- vs2010版本注释
转:http://www.cnblogs.com/chaselwang/p/3580839.html 关于Visual Studio 20**自动添加头部注释信息 作为一个万年潜水党,不关这一篇文章技 ...
- Logstash在Linux上安装部署
Logstash 简介: Logstash 是一个实时数据收集引擎,可收集各类型数据并对其进行分析,过滤和归纳.按照自己条件分析过滤出符合数据导入到可视化界面.它可以实现多样化的数据源数据全量或增量传 ...