android中的onItemSelected微调器

时间:2017-04-05 07:49:08

标签: android android-spinner

public class MainActivity extends Activity implements
    AdapterView.OnItemSelectedListener {

String animalList[] = {"Lion", "Tiger", "Monkey", "Elephant", "Dog", "Cat", "Camel"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Spinner spin = (Spinner) findViewById(R.id.animalNamesSpinner);
    spin.setOnItemSelectedListener(this);


    ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, animalList);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spin.setAdapter(aa);
}


//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

    switch (position){
        case 0:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Lion.class));
            break;

        case 1:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Tiger.class));
            break;

    }
}

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

}

我创造了一个微调器。当我运行应用程序时,它会自动跳转到第一个活动(Lion.class)。我想要的是,在精灵上选择Lion之后,它会进入那个活动。我该如何解决?

我阅读了很多教程,但我找不到答案。我知道这一定很容易

2 个答案:

答案 0 :(得分:0)

onCreate()中添加此内容,因此微调器不会在第一次选择时自动移至新活动。

spin.setAdapter(aa);
spin.setSelection(0, false); //add this

现在在onItemSelected()添加:

 switch (position){
        case 0:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Lion.class));
            break;

        case 1:
            Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, Tiger.class));
            spin.setSelection(0, true);  //add this
            break;

答案 1 :(得分:0)

您可以添加选择项作为此代码的第一个选项

public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {

String animalList[] = {"Select Item","Lion", "Tiger", "Monkey", "Elephant", "Dog", "Cat", "Camel"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner spin = (Spinner) findViewById(R.id.animalNamesSpinner);
spin.setOnItemSelectedListener(this);


ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, animalList);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(aa);
}


//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

switch (position){
    case 1:
        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Lion.class));
        break;

    case 2:
        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Tiger.class));
        break;

}
}

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

}

或添加布尔标志,如下面的代码

    public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
boolean flag = false;
String animalList[] = {"Lion", "Tiger", "Monkey", "Elephant", "Dog", "Cat", "Camel"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner spin = (Spinner) findViewById(R.id.animalNamesSpinner);
spin.setOnItemSelectedListener(this);


ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, animalList);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(aa);
}


//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

switch (position){
    case 0:
    if(flag){

        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Lion.class));
    }
           break;

    case 1:
        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, Tiger.class));
        break;

}
flag = true; 
}

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

}

因为当活动开始时会在第一个pstion和triger onItemSelected中设置一个微调器,所以使用flag你可以避免frist triger