[英]Format Multiple EditTexts with StringBuilder rather than copying code multiple times
I am attempting an Email Application. 我正在尝试电子邮件应用程序。
I have a stringbuilder method that is able to highlight and underline text in an EditText
when the user goes to the next EditText
, like in the screenshot below: 我有一个StringBuilder方法,它能够突出和下划线的文本
EditText
当用户进入下一个EditText
,如下面的截图:
The problem is, to do the above, I have had to copy and paste the code numerous times to do this. 问题是,要执行上述操作,我不得不多次复制并粘贴代码才能执行此操作。 This is the code:
这是代码:
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);
}
}
});
I repeat the code four times as shown below (by renaming edittext
as edittext2
and edittext3
and so on). 我将代码重复四次,如下所示(通过将
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);
}
}
});
The edittexts are declared in the onCreate
method as shown below: 编辑文本在
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
How could I do this with less code, by reading in all the EditText
in one block of code? 通过在一个代码块中读取所有
EditText
,如何用更少的代码来完成此操作?
As suggested by @Commonwares, I created a new class Foo
that implements OnFocusChangeListener
: 正如@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);
}
}
}
Then I can implement it in the onCreate
method like this: 然后,我可以在
onCreate
方法中实现它,如下所示:
Foo test = new Foo();
edittext.setOnFocusChangeListener(test);
edittext2.setOnFocusChangeListener(test);
edittext3.setOnFocusChangeListener(test);
edittext4.setOnFocusChangeListener(test);
How could I do this with less code
我怎么能用更少的代码做到这一点
Use one listener inner class rather than four. 使用一个侦听器内部类,而不是四个。
Each time you do new OnFocusChangeListener()
you are creating a separate class with separate code. 每次执行
new OnFocusChangeListener()
您都将使用单独的代码创建一个单独的类。 Instead, create one such class ( class Foo implements OnFocusChangeListener
) with one onFocusChange()
method. 而是使用一个
onFocusChange()
方法创建一个此类( class Foo implements OnFocusChangeListener
)。 The widget that you need to work with is passed in as the View v
parameter to onFocusChange()
-- just cast that to be an EditText
. 您需要使用的小部件作为
View v
参数传递给onFocusChange()
-只需将其onFocusChange()
转换为EditText
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.