Spinner使用IF else

时间:2016-09-22 08:56:20

标签: android android-spinner

我想在微调器中取值,用户按下ok按钮,然后根据微调器的值启动新活动。字符串数组就是这样。

 public class Menu extends Activity implements View.OnClickListener {
    private String[] array_spinner = {"Select Here", "Honda", "Toyota", "Mitibushi"};
    private String[] array_spinner01 = {"Select Here", "Civic", "Gli", "Lancer"};
    //    //private String[] array_spinner02={"1999","2000","2005"};
//    Spinner s,s1,s2;
//    Button ok;
//@Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
    Spinner s, s1;
    Button ok;
    Object _globalString;
    Object _globalString2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        s = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
        s.setAdapter(adapter);

        s1 = (Spinner) findViewById(R.id.spinner01);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner01);
        s1.setAdapter(adapter1);
//
//    s2=(Spinner)findViewById(R.id.spinner02);
//    ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,array_spinner02);
//    s2.setAdapter(adapter2);

        ok = (Button) findViewById(R.id.btn_ok);
        ok.setOnClickListener(this);

        s.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                _globalString = parent.getItemAtPosition(position);
            }

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

            }
        });

        s1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                _globalString2 = parent.getItemAtPosition(position);
            }

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

            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_ok) {
            if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
                startActivity(new Intent(getApplicationContext(), Civic.class));
            } else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
                startActivity(new Intent(getApplicationContext(), Toyota.class));
            }

        }
    }
}

请告诉我这段代码中我的错误在哪里,因为我坚持了,我无法找到它

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,但我建议您遵循这些解决方案:

第一:

通过在Activity的顶部声明一个字符串,将微调器的值保存在全局字符串变量中,如下所示:

public class Menu extends Activity implements View.OnClickListener {

Spinner s1, s2;
Button acceptbutton;
String _globalString ;
String  _globalString2 ;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
//Your code ..

现在,每当微调器更改值时,您需要将所选字段的值保存到该字符串中:

 s1.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        _globalString = parent.getItemAtPosition(position);
    }

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

    }
});

s.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
        int position, long id) {
    _globalString2 = parent.getItemAtPosition(position);
}

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

}
});

现在您可以根据微调器值切换活动:

   @Override
public void onClick(View v) {
    if (v.getId() == R.id.btn_ok) {
        if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
            startActivity(new Intent(getApplicationContext(), Civic.class));
        } else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
          startActivity(new Intent(getApplicationContext(), Toyota.class));
        }

    }
}