1

所以我是 android 新手,一个问题或更多的烦恼是我的 oncreate 方法

@Override
protected void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.phone_inputscreen);

    presetTypeLbl = (TextView) findViewById(R.id.presetTypeLbl);

    calcBtn = (Button) findViewById(R.id.calcBtn);
    calcBtn.setOnClickListener(this);

    distanceSpinner = (Spinner) findViewById(R.id.distanceSpinner);
    distanceSpinner.setOnItemSelectedListener(this);

    areaSpinner = (Spinner) findViewById(R.id.areaSpinner);
    areaSpinner.setOnItemSelectedListener(this);

    genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
    genderSpinner.setOnItemSelectedListener(this);

    distanceSpinner.setAdapter(createSimpleAdapter(R.array.distanceType));
    areaSpinner.setAdapter(createSimpleAdapter(R.array.areaType));
    genderSpinner.setAdapter(createSimpleAdapter(R.array.genderType));

    shoeSizeInput = (EditText) findViewById(R.id.shoeSizeInput);
    shoeSizeInput.setText("10");

    distanceInput = (EditText) findViewById(R.id.distanceInput);
    distanceInput.setText("0");

    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.RIGHT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    menu.setFadeEnabled(false);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.phone_presetscreen);

    Display display = getWindowManager().getDefaultDisplay();
    @SuppressWarnings("deprecation")
    int width = display.getWidth();
    menu.setBehindOffset(width - 255);

    exv = (ExpandableListView) findViewById(R.id.myList);
    ExListViewAdapter exAdapter = new ExListViewAdapter(this, this);
    exv.setAdapter(exAdapter);

}

正如你所看到的,很多东西只是在 on create 方法中被初始化,我发现这非常......混乱。我想把它放在一个单独的方法中

public void initializeInputScreen() {
    presetTypeLbl = (TextView) findViewById(R.id.presetTypeLbl);

    calcBtn = (Button) findViewById(R.id.calcBtn);
    calcBtn.setOnClickListener(this);

    distanceSpinner = (Spinner) findViewById(R.id.distanceSpinner);
    distanceSpinner.setOnItemSelectedListener(this);

    areaSpinner = (Spinner) findViewById(R.id.areaSpinner);
    areaSpinner.setOnItemSelectedListener(this);

    genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
    genderSpinner.setOnItemSelectedListener(this);

    distanceSpinner.setAdapter(createSimpleAdapter(R.array.distanceType));
    areaSpinner.setAdapter(createSimpleAdapter(R.array.areaType));
    genderSpinner.setAdapter(createSimpleAdapter(R.array.genderType));

    shoeSizeInput = (EditText) findViewById(R.id.shoeSizeInput);
    shoeSizeInput.setText("10");

    distanceInput = (EditText) findViewById(R.id.distanceInput);
    distanceInput.setText("0");

    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.RIGHT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    menu.setFadeEnabled(false);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.phone_presetscreen);

    Display display = getWindowManager().getDefaultDisplay();
    @SuppressWarnings("deprecation")
    int width = display.getWidth();
    menu.setBehindOffset(width - 255);

    exv = (ExpandableListView) findViewById(R.id.myList);
    ExListViewAdapter exAdapter = new ExListViewAdapter(this, this);
    exv.setAdapter(exAdapter);
}

然后我可以通过

protected void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.phone_inputscreen);
    initializeInputScreen();

}

我想知道其他程序员如何一次初始化许多东西,如果有的话,还有什么更简单的方法。

4

1 回答 1

2

没关系。尝试尽可能多地在 XML 中定义事物。例如,设置EditText文本,您不需要在代码中执行此操作。

您甚至可以将设置过程分解为更多的块,例如setupMenu()initializeList().

我尽可能多地尝试这样做 - 好处是可读性。

阅读Clean Code - 有些东西有点矫枉过正,但我​​也尝试将它们应用到 Android 开发中。

于 2013-06-06T05:10:20.727 回答