2017-10-09 49 views
0

有另一種方式做到這一點,我的意思是另一種方法或SPRATE方法,減少編碼,我想是做同樣的事情,但在diffreent方式安卓微調衣被合計方法用另一種方式

public static final int spinner1_op_val = 50; 
public static final int spinner2_op_val = 10; 
public static final int spinner3_op_val = 50; 

    Spinner spinner1 = (Spinner)findViewById(R.id.spinner1); 
    ArrayList<Integer> spinner1_options = new ArrayList<>(); 
    for (i = 0; i <= spinner1_op_val; i += 1) { 
     spinner1_options.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> dataAdapter_for_spinner1 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, spinner1_options); 
    dataAdapter_for_spinner1.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner1.setAdapter(dataAdapter_for_spinner1); 
    spinner1.setSelection(4); 
    spinner1.setOnItemSelectedListener(this); 





    Spinner spinner2 = (Spinner) findViewById(R.id.spinner2); 
    ArrayList<Integer> spinner2_options = new ArrayList<>(); 
    for (i = 0; i <= spinner2_op_val; i++) { 
     spinner2_options.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> dataAdapter_for_spinner2 = new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,spinner2_options); 
    dataAdapter_for_spinner2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner2.setSelection(2); 
    spinner2.setAdapter(dataAdapter_for_spinner2); 
    spinner2.setOnItemSelectedListener(this); 



    Spinner spinner3 = (Spinner) findViewById(R.id.spinner3); 
    ArrayList<Integer> spinner3_option = new ArrayList<>(); 
    for (i = 0; i <= spinner3_op_val; i += 5) { 
     spinner3_option.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> dataAdapter_for_spinner3 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, spinner3_option); 
    dataAdapter_for_spinner3.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner3.setAdapter(dataAdapter_for_spinner3); 
    spinner3.setSelection(2); 
    spinner3.setOnItemSe 

    lectedListener(this); 

extraextraextraextraextraextraextraextraextraextraextraextraextraextraextraextraextraextraextra

回答

0

是的,你可以最大限度地減少你的代碼只需創建這樣的功能。

public void setSpinnerItem(Spinner spinner, int value, int jump, int selection) { 
    ArrayList<Integer> spinnerOption = new ArrayList<>(); 
    for (int i = 0; i < value; i+=jump) { 
     spinnerOption.add(Integer.valueOf(i)); 
    } 
    ArrayAdapter<Integer> integerArrayAdapter = new ArrayAdapter<Integer>(this, R.layout.support_simple_spinner_dropdown_item, spinnerOption); 
    integerArrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); 
    spinner.setAdapter(integerArrayAdapter); 
    spinner.setSelection(selection); 
    spinner.setOnItemSelectedListener(this); 
} 

即可使用該功能。

Spinner spinner1 = (Spinner)findViewById(R.id.spinner1); 
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2); 
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3); 

setSpinnerItem(spinner1, spinner_op_val, forLoopJump, spinnerSelection); 
setSpinnerItem(spinner2, spinner_op_val, forLoopJump, spinnerSelection); 
setSpinnerItem(spinner3, spinner_op_val, forLoopJump, spinnerSelection);