我有一个输入字段我希望它输入密码直到字符串长度为8,之后输入类型应该更改为文本,这意味着如果输入字段有12个字符,则起始8个字符应该是星号和其他字符4应该是文字。 我现在使用的方法现在改变了整个edittext的类型,但我不想这样做。
etEmail.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
email = etEmail.getText().toString();
}
@Override
public void afterTextChanged(Editable editable) {
email = etEmail.getText().toString();
if(email.length() > 8){
etEmail.setTransformationMethod(null);
etEmail.setSelection(etEmail.length());
}
}
});
如果这是字符串:123456789012 输入字段应显示如下:******** 9012
答案 0 :(得分:1)
单个EditText中的可见密码和密码都不可能。
如果您想根据字数计数(当其达到8时)使用 TextWatcher onTextChanged 方法更改整个edittext(可见密码或密码)。
注意:editText可能是密码或可见.cant将单个EditText中的属性组合在一起。但是我们可以使用自定义方式将8以上的字符区分开来
editText.addTextChangedListener(textWatcher);
private final TextWatcher textWatcher= new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// if(s.length()==8)
// setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
//or replace some text above char 8
if(s.length()>=8)
{
s = s.replace(s.substring(8,s.length()), "replace some special character");
editText.setText(s);
}
}
public void afterTextChanged(Editable s) {
}
};
答案 1 :(得分:0)
使用下面我尝试过的代码并按照您的建议获得预期结果。
public class MainActivity extends AppCompatActivity {
EditText et;
TextWatcher myTextWatcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = findViewById(R.id.et);
myTextWatcher = new MyTextListener();
et.addTextChangedListener(myTextWatcher);
}
private void updateEditText(String text) {
if (text.length() < 9) {
String converted="";
for (int a=0; a< text.length(); a++) {
converted = converted + "*";
}
et.removeTextChangedListener(myTextWatcher);
et.setText(converted);
et.setSelection(text.length());
et.addTextChangedListener(myTextWatcher);
}
}
private class MyTextListener implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
updateEditText(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
}
}
对于xml使用xml代码
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
希望能解决你的问题。如果您发现任何问题必须与我分享。
答案 2 :(得分:0)
您需要为空例外和字符长度添加案例,并根据您的要求添加特殊情况,但这是您请求的查询的基本实现
etUsername.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() != 0 && charSequence.length() <= 8 && i < charSequence.length() ) {
updatedString = etUsername.getText().toString().substring(0,charSequence.length()).replaceAll("[0-9]", "*");
etUsername.removeTextChangedListener(this);
etUsername.setText(updatedString);
etUsername.setSelection(charSequence.length());
etUsername.addTextChangedListener(this);
}
}
@Override
public void afterTextChanged(Editable editable) {
/* if (etUsername.length() != 0 ){
updatedString = etUsername.getText().toString().substring(0,(etUsername.length() > 8 ? 8 : etUsername.length())).replaceAll("[0-9]", "*");
etUsername.removeTextChangedListener(this);
etUsername.setText(updatedString);
etUsername.setSelection(etUsername.length());
etUsername.addTextChangedListener(this);
}*/
}
});
我添加的检查以避免空指针异常
updatedString = etUsername.getText().toString().substring(0,charSequence.length()).replaceAll("[0-9]", "*");
在 replaceAll(字符串正则表达式,字符串替换)中,您可以提供任何正则表达式,而且您可以针对性能进行更多优化我只是为您提供基本概念。
答案 3 :(得分:0)
试试这个我为你做了并检查了......它运作正常..
etEmail.addTextChangedListener(new TextWatcher() {
String originalText = "";
String newCharacter = "";
String starCharacter = "";
@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(s.length()>0) {
if(originalText.length()<s.length()) {
newCharacter = s.toString().substring(s.toString().length() - 1);
}else{
newCharacter = "";
}
}else{
newCharacter = "";
originalText = "";
}
}
@Override
public void afterTextChanged(Editable s) {
if(s.length()>0){
//Lock the listener
etEmail.removeTextChangedListener(this);
if(s.length()<=8){
starCharacter = "";
for (int i = 0; i <s.length() ; i++) {
starCharacter += "*";
}
etEmail.setText(starCharacter);
originalText+=newCharacter;
}else{
if(!newCharacter.equals("")) {
originalText += newCharacter;
}else{
originalText = originalText.substring(0, originalText.length()-1);
}
starCharacter = "********";
etEmail.setText(starCharacter + originalText.substring(8));
}
etEmail.setSelection(etEmail.getText().toString().length());
//Give back the listener
etEmail.addTextChangedListener(this);
}else{
originalText = "";
}
}
});
祝你好运..