使用微调器和按钮打开新活动

时间:2014-11-16 01:11:47

标签: android

我基本上有一个微调器显示三个项目。我想要它,以便如果点击这三个项目中的一个,然后单击一个按钮,它将打开一个新的活动。

 String[] count;
Spinner s;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button view = (Button) findViewById(R.id.button);
    s = (Spinner)findViewById(R.id.spinner);
    count = getResources().getStringArray(R.array.country_array);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,count);
    s.setAdapter(adapter);
    view.setEnabled(false); // initially disable the button
    s.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                                   long id) {
            view.setEnabled(true); // enable when user selects any item
        }
        // Listen to button click
        view.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                //start activity
                startActivity();
            }
        }
    });
    public void startActivity() {
        Intent intent = new Intent(YourCurrentActivity.this, NewActivity.class);
        startActivity(intent);
    }

1 个答案:

答案 0 :(得分:0)

view.setEnabled(false); // initially disable the button
s.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
         view.setEnabled(true); // enable when user selects any item
    }

了解如何收听按钮点击:Android: how to handle button click

// Listen to button click
view.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                //start activity
                startActivity();
            }
        });


public void startActivity() {
    Intent intent = new Intent(YourCurrentActivity.this, NewActivity.class);
    startActivity(intent);
}