所以我是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();
}
我想知道其他程序员如何同时初始化大量事物,如果有的话,会有更简单的方法。
答案 0 :(得分:2)
没关系。尝试在XML中尽可能多地定义事物。例如,设置EditText
文字,您不需要在代码中执行此操作。
您甚至可以将设置过程细分为setupMenu()
或initializeList()
等更多块。
我尝试尽可能地做到这一点 - 好处是可读性。
阅读Clean Code - 有些事情有点矫枉过正,但我也尝试将它们应用到Android开发中。