2

每隔一段时间,输入一个新的 Activity 需要更新一些值,比如说几个TextViews。因此,假设我在启动 Activity 的 n Strings 中写入了 n TextViews。

哪一种是保证良好性能和提供“干净代码”的最佳方法?

变体 1(我实际应用的方式):我将单个TextView变量“tempText”声明为全局变量,并将 TextView 分配给该变量(或者使用额外的方法)。或者,a) 在 中执行整个过程onCreate(),而 b) 在名为 eg 的方法中处理所有内容updateTextViews()

(...)

public class MyActivity extends Activity{

    private TextView tempText;

    public onCreate(Bundle icicle){
        (...)

        tempText = (TextView) findViewById(R.id.tv_1);
        tempText.setText(string_1);

        tempText = (TextView) findViewById(R.id.tv_2);
        tempText.setText(string_2);

        (...)

        tempText = (TextView) findViewById(R.id.tv_n);
        tempText.setText(string_n);
    }
}

变体 2:我将单个TextView变量“tempText”声明为onCreate()或相应方法中的变量,并将 TextView 分配给该变量以更新。其余与变体 1 类似。

(...)

public class MyActivity extends Activity{

    public onCreate(Bundle icicle){
        (...)

        private TextView tempText;

        tempText = (TextView) findViewById(R.id.tv_1);
        tempText.setText(string_1);

        tempText = (TextView) findViewById(R.id.tv_2);
        tempText.setText(string_2);

        (...)

        tempText = (TextView) findViewById(R.id.tv_n);
        tempText.setText(string_n);
    }
}

变体 3:TextView我为每个TextView更新 声明了一个全局变量。据我所知,这需要更多的 RAM 空间,但我不知道对速度的影响。onCreate()同样在这里,在(a))或单独的方法(b))中处理它有区别吗?

(...)

public class MyActivity extends Activity{

    private TextView tempText_1;
    private TextView tempText_2;
    (...)
    private TextView tempText_n;

    public onCreate(Bundle icicle){
        (...)

        tempText_1 = (TextView) findViewById(R.id.tv_1);
        tempText_1.setText(string_1);

        tempText_2 = (TextView) findViewById(R.id.tv_2);
        tempText_2.setText(string_2);

        (...)

        tempText_n = (TextView) findViewById(R.id.tv_n);
        tempText_n.setText(string_n);
    }
}

变体 4: 我声明了一个TextView变量,以便在处理此问题的或相应的方法中TextView更新。onCreate()其余的类似于 Variant 3?

(...)

public class MyActivity extends Activity{


    public onCreate(Bundle icicle){
        (...)

        private TextView tempText_1;
        private TextView tempText_2;
        (...)
        private TextView tempText_n;

        tempText_1 = (TextView) findViewById(R.id.tv_1);
        tempText_1.setText(string_1);

        tempText_2 = (TextView) findViewById(R.id.tv_2);
        tempText_2.setText(string_2);

        (...)

        tempText_n = (TextView) findViewById(R.id.tv_n);
        tempText_n.setText(string_n);
    }
}

哪一种是“最好”的方法?变体 1 和 2 仅在 RAM 中保留一个内存地址并使用它,而根据 Robert C. Martins “清洁代码”的说法,这些变量确实是模棱两可的。选项 3 和 4 正好相反。但对于其余部分,我不太清楚其他影响。

4

2 回答 2

3

就个人而言,如果我需要在 Activity 的其他地方再次访问 TextViews(即在 onResume 中,我通常将静态 UI 更新放在这里),我会使用 Variant 3。如果 setText 是一次性的,我会做类似的事情——

((TextView) findViewById( R.id.tv_1 ) ).setText( string_1 );
((TextView) findViewById( R.id.tv_2 ) ).setText( string_2 );

如果对视图可能不存在有任何疑问(即当您在开发期间修改布局时),我会使用包装器,例如 -

private void safeSetText( int id, String caption ) {
    TextView tv = (TextView) findViewById( id );
    if( tv != null )
        tv.setText( caption );
    else
        Log.d( "..... " );
}

然后在 onCreate: safeSetText(R.id.tv_1, string_1); safeSetText(R.id.tv_2, string_2);

于 2012-10-11T14:57:31.900 回答
1

如果您想在整个活动中使用该值,请像往常一样将其声明为类中的成员变量。没有什么像你在 OnCreate 中声明的那样,它仍然是一个成员变量。就像在类体中声明一样,它会提高可读性。

此外,如果您在任何地方声明成员变量,它将占用相同数量的 ram。你在那里没有任何收获/失去。

如果您确定一个变量将仅在本地使用,请在本地声明它,它将分配在堆栈而不是堆中,并且一旦您退出块,它就会消失。

没有这样的 android 优化方式来声明变量。您可以遵循正常的面向对象/Java 做事方式。

于 2012-10-11T15:09:00.863 回答