0

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

我有一个 stringbuilder 方法,它能够EditText在用户转到 next 时突出显示文本并加下划线EditText,如下面的屏幕截图所示:

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

            }

        }
    });

我重复代码四次,如下所示(通过重命名edittextedittext2edittext3)。

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

1 回答 1

1

我怎么能用更少的代码做到这一点

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

每次您这样做时,您都在使用单独的代码new OnFocusChangeListener()创建一个单独的类。相反,使用一种方法创建一个这样的类 ( ) 。您需要使用的小部件作为参数传入- 只需将其转换为.class Foo implements OnFocusChangeListeneronFocusChange()View vonFocusChange()EditText

于 2013-09-30T21:11:53.163 回答