将不可编辑的常量文本放入 EditText 中 - Android [英] Put constant text inside EditText which should be non-editable - Android

查看:24
本文介绍了将不可编辑的常量文本放入 EditText 中 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 editText 中包含常量文本,例如:

I want to have constant text inside editText like:

http://<here_user_can_write>

用户应该无法从http://"中删除任何字符,我搜索了这个并找到了这个:

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

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) : "";
        }
    }
}); 

但我不知道它是否限制用户从开始到结束限制不删除任何字符.我也无法理解 Spanned 类的使用.

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.

如果我们可以将 TextView 放在 EditText 中,那么一种方法将是一个不错的选择,但我认为在 Android 中这是不可能的,因为两者都是视图,是吗?可能吗?

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?

推荐答案

你试过这个方法了吗?

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());

            }

        }
    });

这篇关于将不可编辑的常量文本放入 EditText 中 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆