我已经设置了一个带有一个编辑文本框的对话框。我对允许的最大字符数使用了设置过滤器,但我没有看到最小的过滤器。如果用户输入少于 8 个字符,我添加了代码以在输入的文本中添加“x”。代码编译但崩溃。并抛出错误是“字符串长于 9”。并给出 (16 aspiece.length())。可以使用一些帮助解决
public void onClick(View item) {
final EditText input = new EditText(this);
switch (item.getId()) {
case R.id.textView1:
strEditText = Title1.getText().toString(); // OK
input.setText(strEditText);
input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) }); // Set max # of charactures for Input
for (int i = 0; i < strEditText.length(); i++) { //
String piece = strEditText; //
String toBeAdded = ""; //
if (piece.length() < 8) {
int length = piece.length();
toBeAdded = piece;
while (length < 8) {
toBeAdded = toBeAdded + "X";
length++;
}
} else if (piece.length() > 8) {
throw new IllegalArgumentException("Error, string longer than 9");
} else {
toBeAdded = piece;
}
strEditText = strEditText + toBeAdded;
}// return to for-loop //
new AlertDialog.Builder(ElsEditTitles.this)
.setTitle("Ttile").setView(input).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
strReturn = input.getText().toString();
Title1.setText(strReturn);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
break;