I want to have constant text inside editText like:

我想在editText中有常量文本,比如:

http://<here_user_can_write>

User should not be able to delete any chars from "http://", I searched about this and found this:

用户不应该删除任何来自“http://”的字符,我搜索了这个发现:

editText.setFilters(new InputFilter[] {
    new InputFilter() {
        public CharSequence filter(CharSequence src, int start,
            int end, Spanned dst, int dstart, int dend) {
            return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
        }
    }
}); 

but I don't know whether it restricts user to not delete any chars from start to end limit. I also could not understand use of Spanned class.

但是我不知道它是否限制用户从开始到结束不删除任何字符。我也不明白跨度类的用法。

One way would be a good choice if we can put a TextView inside EditText but I don't think it is possible in Android since both are Views, is it possible?

一种方法是在EditText中放置一个TextView但我认为这在Android中是不可能的,因为两者都是视图,可能吗?

10 个解决方案

#1


92

Did u try this method?

你试过这种方法吗?

final EditText edt = (EditText) findViewById(R.id.editText1);

edt.setText("http://");
Selection.setSelection(edt.getText(), edt.getText().length());


edt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!s.toString().startsWith("http://")){
                edt.setText("http://");
                Selection.setSelection(edt.getText(), edt.getText().length());

            }

        }
    });

更多相关文章

  1. 算法 - 给出一个字符串str,输出包含两个字符串str的最短字符串,如
  2. java漏洞成黑客目标微软呼吁用户更新软件
  3. Java字符与整形的转换问题
  4. java中 16进制字符串 与普通字符串 与 byte数组 之间的转化
  5. Jsp以get方式提交中文及特殊字符,javascript处理乱码问题
  6. 忽略转义字符时拆分字符串
  7. 使用字符串参数调用AndroidJni静态方法。
  8. 用户的身份证号和手机号验证
  9. java中匹配字符串中的中文字符(含中文标点的)

随机推荐

  1. FreeMarker 概述
  2. 有了Python,我能叫出所有猫的名字
  3. Android(安卓)Studio查看Android(安卓)5.
  4. 排序算法 #6 快速排序
  5. 为什么要用Dubbo?
  6. Python字符串必备速查表.pdf
  7. 排序算法 #3 插入排序
  8. 给Python学习者的文件读写指南(含基础与进
  9. 每日前端夜话(0x02):ECMAScript 2016,2017
  10. 排序算法 #7 双路快排