我正在尝试电子邮件应用程序。
我有一个 stringbuilder 方法,它能够EditText
在用户转到 next 时突出显示文本并加下划线EditText
,如下面的屏幕截图所示:
问题是,要执行上述操作,我必须多次复制和粘贴代码才能做到这一点。这是代码:
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundColor(Color.WHITE);
((EditText) v).setTextColor(Color.BLACK);
} else {
/* Below highlights the email addresses after the user has entered them and moved on*/
SpannableStringBuilder emailFormat = new SpannableStringBuilder();
String emailFormatted = edittext.getText().toString();
SpannableString formattedString= new SpannableString(emailFormatted);
formattedString.setSpan(new BackgroundColorSpan(Color.rgb(238,233,233)), 0, emailFormatted.length(), 0);
formattedString.setSpan(new UnderlineSpan(), 0, emailFormatted.length(), 0);
emailFormat.append(formattedString);
edittext.setText(emailFormat, BufferType.SPANNABLE);
}
}
});
我重复代码四次,如下所示(通过重命名edittext
为edittext2
等edittext3
)。
edittext2.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundColor(Color.WHITE);
((EditText) v).setTextColor(Color.BLACK);
} else {
/* Below highlights the email addresses after the user has entered them and moved on*/
SpannableStringBuilder emailFormat = new SpannableStringBuilder();
String emailFormatted = edittext2.getText().toString();
SpannableString formattedString= new SpannableString(emailFormatted);
formattedString.setSpan(new BackgroundColorSpan(Color.rgb(238,233,233)), 0, emailFormatted.length(), 0);
formattedString.setSpan(new UnderlineSpan(), 0, emailFormatted.length(), 0);
emailFormat.append(formattedString);
edittext2.setText(emailFormat, BufferType.SPANNABLE);
}
}
});
编辑文本在onCreate
方法中声明,如下所示:
edittext = (EditText) findViewById(R.id.editText1); // From
edittext2 = (EditText) findViewById(R.id.editText2); // To
edittext3 = (EditText) findViewById(R.id.editText3); // cc
edittext4 = (EditText) findViewById(R.id.editText4); // bcc
通过在一个代码块中读取所有代码,我怎么能用更少的代码来做到这一点EditText
?
解决方案:
正如@Commonwares 所建议的,我创建了一个Foo
实现的新类OnFocusChangeListener
:
class Foo implements OnFocusChangeListener{
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v = (EditText) v;
if (hasFocus) {
v.setBackgroundColor(Color.WHITE);
((EditText) v).setTextColor(Color.BLACK);
} else {
/* Below highlights the email addresses after the user has entered them and moved on*/
SpannableStringBuilder emailFormat = new SpannableStringBuilder();
String emailFormatted = ((EditText) v).getText().toString();
SpannableString formattedString= new SpannableString(emailFormatted);
formattedString.setSpan(new BackgroundColorSpan(Color.rgb(238,233,233)), 0, emailFormatted.length(), 0);
formattedString.setSpan(new UnderlineSpan(), 0, emailFormatted.length(), 0);
emailFormat.append(formattedString);
((EditText) v).setText(emailFormat, BufferType.SPANNABLE);
}
}
}
然后我可以在这样的onCreate
方法中实现它:
Foo test = new Foo();
edittext.setOnFocusChangeListener(test);
edittext2.setOnFocusChangeListener(test);
edittext3.setOnFocusChangeListener(test);
edittext4.setOnFocusChangeListener(test);