私は電子メールアプリケーションを試みています。
以下のスクリーンショットのようにEditText
、ユーザーが nextに移動したときにテキストを強調表示して下線を引くことができる stringbuilder メソッドがあります。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);
}
}
});
以下に示すように、コードを 4 回繰り返します (名前edittext
を asedittext2
などに変更し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);
}
}
});
edittext は、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
1 つのコード ブロックですべてを読み取ることにより、少ないコードでこれを行うにはどうすればよいでしょうか?
解決:
@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);