0

私は電子メールアプリケーションを試みています。

以下のスクリーンショットのようにEditText、ユーザーが nextに移動したときにテキストを強調表示して下線を引くことができる stringbuilder メソッドがあります。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);

            }

        }
    });

以下に示すように、コードを 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

EditText1 つのコード ブロックですべてを読み取ることにより、少ないコードでこれを行うにはどうすればよいでしょうか?

解決:

@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

より少ないコードでこれを行うにはどうすればよいですか

4 つではなく、1 つのリスナー内部クラスを使用します。

そのたびに、のコードでのクラスをnew OnFocusChangeListener()作成しています。代わりに、1 つのメソッドでそのようなクラス ( ) を 1 つ作成します。操作する必要があるウィジェットは、パラメーターとして に渡されます。それを.class Foo implements OnFocusChangeListeneronFocusChange()View vonFocusChange()EditText

于 2013-09-30T21:11:53.163 に答える