2017-09-28 49 views
0

首先,我對此很新,所以請耐心等待。 我想做一個微調,如果你選擇一個項目,佈局和活動會改變,但我甚至不能實現最簡單的微調。改變微調器的佈局和活動

到目前爲止,我已經得到的唯一代碼是下面的代碼,但不管我輸入什麼都沒有做任何事情,所以我必須不理解它,請在您的答案中非常具體。謝謝。第一個答案了我一些方法,但「旋轉器的旋轉..... spin.setAdapter(AA)」部分未在OnCreate

String[] generations = { "Gen2", "Gen3", "Gen4", "Gen5", "Gen6","Gen7" }; 

Spinner spin = (Spinner) findViewById(R.id.spinner1); 
spin.setOnItemSelectedListener(this); 
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,generations); 
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
//Setting the ArrayAdapter data on the Spinner 
spin.setAdapter(aa); 

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    switch (position) { 
     case 1: 
      Intent intent2 = new Intent(this, gen2.class); 
      startActivity(intent2); 
      break; 
     case 2: 
      Intent intent3 = new Intent(this, Gen3.class); 
      startActivity(intent3); 
      break; 
     case 3: 
      Intent intent4 = new Intent(this, gen4.class); 
      startActivity(intent4); 
      break; 
     case 4: 
      Intent intent5 = new Intent(this, gen5.class); 
      startActivity(intent5); 
      break; 
     case 5: 
      Intent intent6 = new Intent(this, MainActivity.class); 
      startActivity(intent6); 
      break; 
     case 6: 
      Intent intent7 = new Intent(this, gen7.class); 
      startActivity(intent7); 
      break; 
    } 
} 

回答

0

這裏下接受是一個例子,希望它可以幫助你理解它更好:

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener { 

String[] country = { "India", "USA", "China", "Japan", "Other", }; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //Getting the instance of Spinner and applying OnItemSelectedListener on it 
    Spinner spin = (Spinner) findViewById(R.id.spinner1); 
    spin.setOnItemSelectedListener(this); 

    //Creating the ArrayAdapter instance having the country list 
    ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country); 
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    //Setting the ArrayAdapter data on the Spinner 
    spin.setAdapter(aa); 
} 


//Performing action onItemSelected and onNothing selected 
@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) { 
    switch (position){ 
     // Check what position was selected 
     case 1: 
     //start activity depending on position 
     Intent intent = new Intent(this, ActivityOne.class); 
     startActivity(intent); 
     break; 

     case 2: 
     Intent intent = new Intent(this, ActivityTwo.class); 
     startActivity(intent); 
     break; 

     case 3: 
     Intent intent = new Intent(this, ActivityThree.class); 
     startActivity(intent); 
     break; 
} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
    } 
} 
+0

它讓我更接近,所以非常感謝你。 它仍然不會改變任何東西,它不會改變佈局,它不會改變類 – Runesen