2011-06-23 43 views
0

我想要在用戶在微調器上選擇「組合」時出現EditText對象,我該怎麼做?用戶輸入導致窗體出現

這是我一直在努力:

 ground = (Spinner) findViewById(R.id.ground); 
    ArrayAdapter<CharSequence> groundAdapter = ArrayAdapter.createFromResource(
      this, R.array.ground_array, android.R.layout.simple_spinner_item); 
    groundAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    ground.setAdapter(groundAdapter); 
    ground.setOnItemSelectedListener(new GroundListener()); 
    if(ground.getSelectedItem().toString().equalsIgnoreCase("Combination")) 
    { 
     combo.setVisibility(0); 
    } 

的對象的EditText組合在XML文件中設置爲android:visibility="gone"

GroundListener代碼是

 public class GroundListener implements OnItemSelectedListener { 

      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
       String selected = parent.getItemAtPosition(pos).toString(); 
      } 

      public void onNothingSelected(AdapterView parent) 
      { 
       // Do nothing. 
      } 
     } 

回答

1

什麼是GroundListener?

您不應該使用AdapterView.OnItemSelectedListener及其onItemSelected方法嗎?

除了使用setVisibility(View.VISIBLE)而不是0來提高可讀性。

編輯:

我不明白你與你的代碼做什麼,你GroundListener未插入到任何東西,你的測試監聽器之外。

嘗試:

ground.setOnItemSelectedListener(new OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 

      if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Combination")) 
      { 
       combo.setVisibility(View.VISIBLE); 
      } 
     } 

     public void onNothingSelected(AdapterView parent) 
     { 
      // Do nothing. 
     } 
    }); 

檢查其是否正常工作,然後帶回的代碼在你的GroundListener,看看它是否工作。儘管GroundListener可能不知道什麼是組合,但您可能會遇到問題。但你會解決這個問題。

編輯:

語法更正

+0

「公共類GroundListener實現OnItemSelectedListener { \t公共無效onItemSelected(適配器視圖父,瀏覽視圖,整數POS,長ID){ \t字符串選擇= parent.getItemAtPosition(POS)的ToString(); \t} \t公共無效onNothingSelected(適配器視圖父) \t { \t //什麼也不做。 \t} \t}'這就是GroundListener。對不起,應該包括在問題中。現在編輯 – Zaask

+0

我仍然不知道該怎麼辦 – Zaask

+0

我已經編輯了我的答案給你。 – Yahel