使用StringBuilder格式化多个EditTexts,而不是多次复制代码

时间:2013-09-30 21:07:29

标签: java android

我正在尝试使用电子邮件应用程序。

我有一个stringbuilder方法,当用户转到下一个EditText时,该方法可以突出显示EditText中的文字并加下划线,如下面的屏幕截图所示:

Format Text with StringBuilder

问题是,为了做到这一点,我不得不多次复制并粘贴代码来执行此操作。这是代码:

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重命名为edittext2edittext3等等。)

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);

            }

        }
    });

edittexts在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);

1 个答案:

答案 0 :(得分:1)

  

我怎么能用更少的代码

来做到这一点

使用一个侦听器内部类而不是四个。

每次执行new OnFocusChangeListener()时,您都会使用单独的代码创建单独的类。相反,使用一个class Foo implements OnFocusChangeListener方法创建一个这样的类(onFocusChange())。您需要使用的窗口小部件作为View v参数传递给onFocusChange() - 只需将其转换为EditText