把常量文本内的EditText应该是不可编辑 - 机器人 [英] Put constant text inside EditText which should be non-editable - Android

查看:98
本文介绍了把常量文本内的EditText应该是不可编辑 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有内部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) : "";
        }
    }
}); 

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

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?

推荐答案

难道ü尝试这种方法吗?

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().contains("http://")){
                edt.setText("http://");
                Selection.setSelection(edt.getText(), edt.getText().length());

            }

        }
    });

这篇关于把常量文本内的EditText应该是不可编辑 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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