0

我想为 Spinner 做 if else 声明。我宣布“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”

当我单击“August”时,它会给我 MainActivity.java,否则它会给我 August.java。现在,我的微调器是自动选择的,这意味着,当我加载页面时,它会自动为我选择“六月”。有没有办法让我禁用自动选择?

这是我的代码

String [] months = {
                "June",
                "July",
                "August",
                "Septemeber",
                "November",
                "December",

我的 date.java 的一部分

//SpinnerView
            s1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, months);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                //Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show();
            }
            public void onNothingSelected(AdapterView<?>arg0) {}
            });


    if ( s1.equals("August")) {

        startActivity(new Intent(date.this,MainActivity.class));
    }
    else{
        startActivity(new Intent(date.this,august
                .class));

    }
4

3 回答 3

2

尝试这个:

添加数组,如“选择月份”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”

并签到

s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                if(index>0)
                {
                             String Month = MonthArray[index];
                    if ( Month.equalIgnoreCase("August")) {

                        startActivity(new Intent(date.this,MainActivity.class));
                    }
                    else{
                        startActivity(new Intent(date.this,august
                                .class));

                    }
                }
            }
            public void onNothingSelected(AdapterView<?>arg0) {}
        });

希望能帮助到你!!

于 2013-08-16T04:56:49.873 回答
2

可能这会有所帮助

    import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class date extends Activity implements AdapterView.OnItemSelectedListener{



    ArrayList<String> months;

    Spinner spinner;

    ArrayAdapter<String> month_adapter;

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


        months = new ArrayList<String>();

        months.add("Select");
        months.add("June");
        months.add("July");
        months.add("August");
        months.add("Septemeber");
        months.add("November");
        months.add("December");

        spinner = (Spinner) findViewById(R.id.spinner); 
        spinner.setOnItemSelectedListener(this);


        month_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, months);
        month_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(month_adapter);


    }

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

    @Override
    public void onItemSelected(AdapterView<?> arg0, View v, int position,
            long arg3) {
        // TODO Auto-generated method stub


        if(!(spinner.getSelectedItem().toString().trim().equals("Select")))
        {

            if ( spinner.getSelectedItem().toString().trim().equals("August")) {

                startActivity(new Intent(date.this,MainActivity.class));
            }
            else{
                startActivity(new Intent(date.this,august
                            .class));

            }

        }

    }

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

    }

}
于 2013-08-16T05:08:03.797 回答
0

您需要将Spinner最后选择的项目位置存储在您的数据库或SharedPreference. 当您的应用程序加载时,检查您的项目位置是否在您的数据库或中SharedPreference,然后Spinner根据此位置选择项目中的项目。

于 2013-08-16T04:54:45.577 回答