如何在Android中为固定大小的字符串制作带有下划线的Edittext?
我需要制作一个EditText,当用户添加新的字符时,用一个下划线替换一个字符。大概是这样的: 我做了一些类似的事情,有6个EditText(所需字符串的大小)和一个“u”字符作为提示,a在其中写入时将焦点更改为下一个,删除时将焦点更改为上一个,但在删除或编辑未添加的最后一个字符时,我遇到了问题。 有人知道我怎样才能做好吗 这是我在活动中的代码:如何在Android中为固定大小的字符串制作带有下划线的Edittext?,android,android-edittext,Android,Android Edittext,我需要制作一个EditText,当用户添加新的字符时,用一个下划线替换一个字符。大概是这样的: 我做了一些类似的事情,有6个EditText(所需字符串的大小)和一个“u”字符作为提示,a在其中写入时将焦点更改为下一个,删除时将焦点更改为上一个,但在删除或编辑未添加的最后一个字符时,我遇到了问题。 有人知道我怎样才能做好吗 这是我在活动中的代码: private void manageFocus(final EditText beforeET, final EditText curren
private void manageFocus(final EditText beforeET, final EditText currenteET, final EditText afterET) {
if (beforeET != null) {
currenteET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && beforeET != null && beforeET.getText().toString().length() < 1 && currenteET.getText().toString().equals("")) {
beforeET.requestFocus();
}
}
});
}
currenteET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (afterET != null && currenteET.getText().toString().length() >= 1) {
afterET.requestFocus();
} else if (beforeET != null && currenteET.getText().toString().length() < 1) {
beforeET.requestFocus();
}
}
});
}
EditText editText;
private String text;
private boolean delete = false;
private static final int CODE_SIZE=6;
private void initTextView() {
text = editText.getText().toString();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
text = editText.getText().toString();
if (count > after)
delete = true;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
StringBuilder sb = new StringBuilder(s.toString());
int replacePosition = editText.getSelectionEnd();
if (s.length() != CODE_SIZE) {
if (!delete) {
if (replacePosition < s.length())
sb.deleteCharAt(replacePosition);
} else {
sb.insert(replacePosition, '_');
}
if (replacePosition < s.length() || delete) {
editText.setText(sb.toString());
editText.setSelection(replacePosition);
} else {
editText.setText(text);
editText.setSelection(replacePosition - 1);
}
}
}
delete = false;
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void manageFocus(前最终编辑文本、当前最终编辑文本、后最终编辑文本){
if(beforeET!=null){
currenteET.setOnFocusChangeListener(新视图.OnFocusChangeListener(){
@凌驾
public void onFocusChange(视图v,布尔hasFocus){
if(hasFocus&&beforeET!=null&&beforeET.getText().toString().length()<1&¤teET.getText().toString().equals(“”){
beforeET.requestFocus();
}
}
});
}
currenteET.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
公共无效后文本已更改(可编辑){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
if(afterET!=null&¤teET.getText().toString().length()>=1){
afterET.requestFocus();
}else if(beforeET!=null&¤teET.getText().toString().length()<1){
beforeET.requestFocus();
}
}
});
}
布局代码如下:
<LinearLayout
android:id="@+id/login_layout_code"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:visibility="gone">
<EditText
android:id="@+id/login_code_et01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:hint="_"
android:textColorHint="@color/text_gray"
android:gravity="center"
android:textSize="@dimen/textsize_32"
android:inputType="number"
android:maxLength="1"
android:textColor="@color/text_gray"
fontPath="fonts/TitilliumText/TitilliumText22L-Bold.otf"/>
<EditText
android:id="@+id/login_code_et02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="_"
android:textColorHint="@color/text_gray"
android:textSize="@dimen/textsize_32"
android:layout_toRightOf="@id/login_code_et01"
android:inputType="number"
android:maxLength="1"
android:textColor="@color/text_gray"
fontPath="fonts/TitilliumText/TitilliumText22L-Bold.otf"/>
<EditText
android:id="@+id/login_code_et03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="_"
android:textColorHint="@color/text_gray"
android:textSize="@dimen/textsize_32"
android:layout_toRightOf="@id/login_code_et02"
android:inputType="number"
android:maxLength="1"
android:textColor="@color/text_gray"
fontPath="fonts/TitilliumText/TitilliumText22L-Bold.otf"/>
<EditText
android:id="@+id/login_code_et04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="_"
android:textColorHint="@color/text_gray"
android:textSize="@dimen/textsize_32"
android:layout_toRightOf="@id/login_code_et03"
android:inputType="number"
android:maxLength="1"
android:textColor="@color/text_gray"
fontPath="fonts/TitilliumText/TitilliumText22L-Bold.otf"/>
<EditText
android:id="@+id/login_code_et05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="_"
android:textColorHint="@color/text_gray"
android:textSize="@dimen/textsize_32"
android:layout_toRightOf="@id/login_code_et04"
android:inputType="number"
android:maxLength="1"
android:textColor="@color/text_gray"
fontPath="fonts/TitilliumText/TitilliumText22L-Bold.otf"/>
<EditText
android:id="@+id/login_code_et06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="_"
android:textColorHint="@color/text_gray"
android:textSize="@dimen/textsize_32"
android:layout_toRightOf="@id/login_code_et05"
android:inputType="number"
android:maxLength="1"
android:textColor="@color/text_gray"
fontPath="fonts/TitilliumText/TitilliumText22L-Bold.otf"/>
</LinearLayout>
我会尝试在
编辑文本
后面使用文本视图
(假设您的编辑文本有一个透明的背景)来显示下划线,并在编辑文本
中的文本更改时对其进行更新
否则,您可以子类化EditText
,并在onDraw()
中实现一些自定义绘图
我认为这两种方法中的任何一种都比使用6个EditText来管理它们的焦点要好。最后,我发现解决方案只是在EditText中添加一个TextWacher,在添加字符时将一个“”删除到下一个字符,在删除位置时添加一个“” 这是EditText的xml代码:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:singleLine="true"
android:text="______"
android:letterSpacing="0.2"
android:textSize="29sp" />
这是活动中的代码:
private void manageFocus(final EditText beforeET, final EditText currenteET, final EditText afterET) {
if (beforeET != null) {
currenteET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && beforeET != null && beforeET.getText().toString().length() < 1 && currenteET.getText().toString().equals("")) {
beforeET.requestFocus();
}
}
});
}
currenteET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (afterET != null && currenteET.getText().toString().length() >= 1) {
afterET.requestFocus();
} else if (beforeET != null && currenteET.getText().toString().length() < 1) {
beforeET.requestFocus();
}
}
});
}
EditText editText;
private String text;
private boolean delete = false;
private static final int CODE_SIZE=6;
private void initTextView() {
text = editText.getText().toString();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
text = editText.getText().toString();
if (count > after)
delete = true;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
StringBuilder sb = new StringBuilder(s.toString());
int replacePosition = editText.getSelectionEnd();
if (s.length() != CODE_SIZE) {
if (!delete) {
if (replacePosition < s.length())
sb.deleteCharAt(replacePosition);
} else {
sb.insert(replacePosition, '_');
}
if (replacePosition < s.length() || delete) {
editText.setText(sb.toString());
editText.setSelection(replacePosition);
} else {
editText.setText(text);
editText.setSelection(replacePosition - 1);
}
}
}
delete = false;
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
EditText;
私有字符串文本;
私有布尔删除=false;
专用静态最终整数代码_SIZE=6;
私有void initTextView(){
text=editText.getText().toString();
editText.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
text=editText.getText().toString();
如果(计数>之后)
删除=真;
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
StringBuilder sb=新的StringBuilder(s.toString());
int replacePosition=editText.getSelectionEnd();
如果(s.length()!=代码大小){
如果(!删除){
如果(替换位置
此代码在api级别21及更高版本中运行良好,因为letterSpacin仅在api级别21及更高版本中运行。但如果你使用不同于Android股票的字体,它可能会起作用。对于我来说,TilliumText22L Regular适用。谢谢,我写了类似的东西,但是如果我删除了EditText中间的一个字符,该行将出现在EditText的末尾,不会出现在删除的位置。@JachumbeleChauntomantekilla这是一个要求吗?我认为,作为一个用户,如果我在中间删除文本,我的期望是文本的其余部分折叠来填充空间。这就是其他所有文本字段的行为方式,为什么要不同呢?是的是一个要求。如果是我,我会照你说的做。最后,我找到了解决办法。