如何使用foreach循环清理这个冗长的代码补丁?

时间:2012-11-09 01:40:38

标签: java android foreach

Java / Android初学者。我在我的应用程序中使用自定义字体,并为每段文本创建setTypeface()语句会影响我的代码质量。

当前代码:

Typeface Helvetica = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.otf");

final TextView Nike = (TextView)findViewById(R.id.Nike);
final TextView Puma = (TextView)findViewById(R.id.Puma);
final TextView Adidas = (TextView)findViewById(R.id.Adidas);
final TextView Reebok = (TextView)findViewById(R.id.Reebok);
final TextView Umbro = (TextView)findViewById(R.id.Umbro);

Nike.setTypeface(Helvetica);
Puma.setTypeface(Helvetica);
Adidas.setTypeface(Helvetica);
Reebok.setTypeface(Helvetica);
Umbro.setTypeface(Helvetica);

如何缩短此代码?我正在考虑一个带阵列的foreach循环,但我似乎无法弄明白。这样的事情是可能的:

String[] HelveticaTextArray = {"Nike", "Puma", "Adidas", "Reebok", "Umbro"};

for ( String[] HelveticaTextArray : item ) {
    final TextView HelveticaText = (TextView).findViewById(R.id.item);
    HelveticaText.setTypeface(Helvetica);
};

4 个答案:

答案 0 :(得分:2)

由于你为每个TextView做同样的事情,我倾向于创建一个方便的方法来做你需要的,传递R.id.xxx和字体。如,

private void initializeViews() {
    Typeface Helvetica = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.otf");

    final TextView Nike = initializeTextView(R.id.Nike, Helvetica);
    final TextView Puma = initializeTextView(R.id.Puma, Helvetica);
    final TextView Adidas = initializeTextView(R.id.Adidas, Helvetica);
    final TextView Reebok = initializeTextView(R.id.Reebok, Helvetica);
    final TextView Umbro = initializeTextView(R.id.Umbro, Helvetica);

}

private TextView initializeTextView(int textViewId, Typeface typeface) {
    TextView textView = (TextView)findViewById(textViewId);
    if (textView != null)
        textView.setTypeface(typeface);
    else
        Log.e(TAG, "some error message");
    return textView;
}

答案 1 :(得分:1)

您可以存储int ID而不是字符串。

//String[] HelveticaTextArray = {"Nike", "Puma", "Adidas", "Reebok", "Umbro"};
int[] HelveticaTextArray = {R.id.Nike, R.id.Puma, R.id.Adidas, R.id.Reebok, R.id.Umbro}


for ( int[] HelveticaTextArray : item ) {
    final TextView HelveticaText = (TextView)findViewById(item);
    HelveticaText.setTypeface(Helvetica);
};

除非你永远不会对这些TextView做任何事情,否则我建议不要这样做。 findViewByID()是一个相对昂贵的调用,如果您以后使用TextView引用,则应该保留它们而不是多次调用findViewById()

将TextView与HelveticaTextView一起子类化并在构造函数中设置字体可能是个更好的主意,然后使用它而不是TextView,你就不必弄乱你的Activity中的字体。

这样的事情:

public class HelveticaTextView extends TextView{
    public HelveticaTextView(Context c){
        Typeface Helvetica = Typeface.createFromAsset(c.getAssets(), "fonts/Helvetica.otf");
        super(c);
        this.setTypeface(Helvetica);
    }
    public HelveticaTextView(Context c, AttributeSet attrs){
    Typeface Helvetica = Typeface.createFromAsset(c.getAssets(), "fonts/Helvetica.otf");
        super(c, attrs);
        this.setTypeface(Helvetica);
    }
    public HelveticaTextView(Context context, AttributeSet attrs, int defStyle) {
    Typeface Helvetica = Typeface.createFromAsset(c.getAssets(), "fonts/Helvetica.otf");
        super(c, attrs, defStyle);
        this.setTypeface(Helvetica);
    }
}

然后在你的xml布局中更改

<TextView .../>

<com.your.package.name.HelveticaTextView .../>

答案 2 :(得分:1)

int[] HelveticaArray = {R.id.Nike, R.id.Puma, R.id.Reebok, R.id.Umbro};

for (int item : HelveticaArray) {
    ((TextView).findViewById(item)).setTypeFace(Helvetica);
}

答案 3 :(得分:1)

可能是这样的:

TextView[] textViews = { (TextView) findViewById(R.id.Nike), (TextView) findViewById(R.id.Puma) };

for (TextView view : textViews) {
   view.setTypeface(Helvetica);
}