Android 下划线行表示要猜测的文本,然后用EditText中的输入文本替换下划线
我有一段文字,例如“这是一个问题”,必须通过一些线索来猜测。 在应用程序中,用户将在EditText中看到“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。所以这就像“这是一个问题” 因为我在这个世界上是个新手,所以我不确定是否必须在EditText中添加一个监听器,它可以将用户输入替换为所需的内容或其他内容 任何回复都将不胜感激Android 下划线行表示要猜测的文本,然后用EditText中的输入文本替换下划线,android,android-edittext,Android,Android Edittext,我有一段文字,例如“这是一个问题”,必须通过一些线索来猜测。 在应用程序中,用户将在EditText中看到“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。所以这就像“这是一个问题” 因为我在这个世界上是个新手,所以我不确定是否必须在EditText中添加一个监听器,它可以将用户输入替换为所需的内容或其他内容
谢谢。每当键入密钥时,请使用此方法:
public static int getIndex(String input) {
return input.replaceAll("_", "").length();
}
public static String prepareOutput(String actual, String input) {
input = input.replaceAll("_", "");
int diff = actual.length() - input.length();
if (diff < 0) {
return input;
}
for (int i = 0; i < diff; i++) {
input += "_";
}
return input;
}
您需要在活动中实现,并重写其方法以执行您想要的操作。
final EditText ed = (EditText) findViewById(R.id.editText1);
ed.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
String output = prepareOutput("This is a question", input);
if (output.compareTo(input) != 0) {
ed.setText(output);
ed.setSelection(getIndex(output));
}
}
});