2014-09-19 39 views
-3

考慮到我有以下AutoCompleteTextViews如何爲多個AutoCompleteTextView設置相同的屬性?

ArrayAdapter<String> adapter121 = new ArrayAdapter<String>  
    (this,android.R.layout.simple_dropdown_item_1line,androidBooks); 
    AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks); 
    AutoCompleteTextView acTextView0 = (AutoCompleteTextView)findViewById(R.id.AndroidBooks1); 
    AutoCompleteTextView acTextView1 = (AutoCompleteTextView)findViewById(R.id.et3); 
    AutoCompleteTextView acTextView2 = (AutoCompleteTextView)findViewById(R.id.et4); 
    AutoCompleteTextView acTextView3 = (AutoCompleteTextView)findViewById(R.id.et5); 
    AutoCompleteTextView acTextView4 = (AutoCompleteTextView)findViewById(R.id.et6); 
    AutoCompleteTextView acTextView5 = (AutoCompleteTextView)findViewById(R.id.et7); 
    AutoCompleteTextView acTextView6 = (AutoCompleteTextView)findViewById(R.id.et8); 
    AutoCompleteTextView acTextView7 = (AutoCompleteTextView)findViewById(R.id.et9); 

我怎麼會叫:

acTextView.setThreshold(1); 
    acTextView.setAdapter(adapter121); 

每個我AutoCompleteTextViews的?

+2

什麼? ?........ – codeMagic 2014-09-19 18:27:39

+0

acTextView0和acTextView1以及acTextView2如何在代碼中啓動,就像acTextView.setThreshold(1); acTextView.setAdapter(adapter121); – 2014-09-19 18:29:18

+1

用您的實際問題更新您的文章。簡單地發佈代碼並不是一個有效的問題。 – codeMagic 2014-09-19 18:29:57

回答

1

要設置AutoCompleteTextViews的每一個,您可以將它們添加到數組mAutoCompleteTextViews並通過循環遍歷每個屬性來設置屬性。

ArrayAdapter<String> adapter121 = new ArrayAdapter<String> 
       (this,android.R.layout.simple_dropdown_item_1line,androidBooks); 

     AutoCompleteTextView[] mAutoCompleteTextViews = new AutoCompleteTextView[{ 
     (AutoCompleteTextView)findViewById(R.id.AndroidBooks), 
     (AutoCompleteTextView)findViewById(R.id.AndroidBooks1), 
     (AutoCompleteTextView)findViewById(R.id.et3), 
     (AutoCompleteTextView)findViewById(R.id.et4), 
     (AutoCompleteTextView)findViewById(R.id.et5), 
     (AutoCompleteTextView)findViewById(R.id.et6), 
     (AutoCompleteTextView)findViewById(R.id.et7), 
     (AutoCompleteTextView)findViewById(R.id.et8), 
     (AutoCompleteTextView)findViewById(R.id.et9)}]; 

     // for each loop to set parameters on each AutoCompleteTextView in mAutoCompleteTextViews 
     for (AutoCompleteTextView acTextView : mAutoCompleteTextViews){ 
      acTextView.setThreshold(1); 
      acTextView.setAdapter(adapter121); 
     } 

如果您想了解在Java數組,然後從Java退房this section維基

+1

謝謝@ MrEnginner13 – 2014-09-19 18:52:04

0

實際上,你可以創建一個循環,它所有這一切都爲你

int numberOfViews = 9; 
for(int i = 0; i < numberOfViews; i++){ 
    AutoComplete acTextView; 
    if(i == 0) 
     acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks); 
    if(i == 1) 
     acTextView = (AutoCompleteTextView)findViewById(R.id.AndroidBooks1); 
    else{ 
     int id = getResources().getIdentifier("et" + (i+1), "id", getPackageName()); 
     acTextView = (AutoCompleteTextView)findViewById(id); 
    } 
    acTextView.setThreshold(1); 
    acTextView.setAdapter(adapter121); 
} 
相關問題