Gre*_*des 7 android substring textview android-edittext
这是我在应用程序中的编辑文本之一
<EditText
android:id="@+id/etFolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/pictures"
android:ems="10" >
Run Code Online (Sandbox Code Playgroud)
当它第一次出现时它带有"/ pictures"
用户可以更改文本并输入另一个单词.但如何防止删除编辑文本的"/".
用户可以删除所有其他文本,但不允许删除第一个字符.
我怎样才能实现这种行为?
您可以将您的 EditText 放在您拥有的主布局中的一个单独的 RelativeLayout 中,并在同一行上添加一个带有文本的 TextView / 就在它之前,并在 EditText 文本中仅保留“图片”部分。像这样的东西:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "/"/>
<EditText
android:id="@+id/etFolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="pictures"
android:ems="10"
android:layout_marginLeft="10dp" >
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
有几种方法可以实现此目的。首先,您可以简单地拥有它,以便如果该EditText块为空,则立即用“ /” char填充它。或者,如果前一个char是/,则进行设置,以防止用户删除。以下代码未经测试,因此您可能需要对其进行一些调整。
editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (editText.getText().charAt(editText.length()-1) == '/') {
editText.append(" ");
}
//OR...
if (editText.length() == 0) {
editText.setText("/")
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Run Code Online (Sandbox Code Playgroud)
编辑: FWIW,如果我在您的位置,我个人会选择像Gabriella和apk结合的解决方案。但是,由于您的问题是特定的,因此我尝试直接回答。
| 归档时间: |
|
| 查看次数: |
7599 次 |
| 最近记录: |