将常量文本放在EditText中,这应该是不可编辑的——Android
16lz
2021-01-22
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());
}
}
});
更多相关文章
- 算法 - 给出一个字符串str,输出包含两个字符串str的最短字符串,如
- java漏洞成黑客目标微软呼吁用户更新软件
- Java字符与整形的转换问题
- java中 16进制字符串 与普通字符串 与 byte数组 之间的转化
- Jsp以get方式提交中文及特殊字符,javascript处理乱码问题
- 忽略转义字符时拆分字符串
- 使用字符串参数调用AndroidJni静态方法。
- 用户的身份证号和手机号验证
- java中匹配字符串中的中文字符(含中文标点的)