所以我是 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();
}
我想知道其他程序员如何一次初始化许多东西,如果有的话,还有什么更简单的方法。