我认为以下代码可以帮助您这样做。
的
解决方案1
</强>
TextView
将只显示前缀文本,和
EditText
可以是用户的选择。
@android:color/white" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://"
android:textColor="@android:color/darker_gray" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:text="www.google.com"
/>
</code>
在上面的布局中,您将看到
TextView
拥有默认文本,但用户只能写入
EditText
。所以用户不会对这种技巧有所了解。
的
解决方案2
</强>
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());
}
}
});
</code>
此解决方案不允许用户从其中删除“http://”
EditText
输入。