将微调器挂钩到onClick Listener,打开新页面

时间:2012-01-15 04:12:13

标签: java android

仍然是一个Android新手,但希望有人可以指出我在这个问题上的正确方向。 我想要实现的是。 我有2个单选按钮,当单击一个时,它会将微调器中的数组更改为加拿大,当我单击其他数组时出现。这很好用! 但我正在努力将onClickListener(从微调器onItemSelect)连接到提交按钮。我想根据所选省份或州发出打开新页面的意图。有些人可以向我展示一个转换声明,我们将非常感激。提前谢谢。

String xml

        <string name="prov_picker">Select a Province</string>
    <string-array name="prov_array">
        <item>-- Select Province --</item>
        <item>British Columbia</item>
        <item>Alberta</item>
        <item>Saskatchewan</item>
        <item>Manitoba</item>
        <item>Ontario</item>
        <item>Quebec</item>
        <item>New Brunswick</item>
        <item>Nova Scotia</item>
        <item>Newfoundland</item>
    </string-array>
     <string-array name="prov1_array">
        <item>-- Select State --</item>
        <item>Washington</item>
        <item>Florida</item>
        <item>California</item>
        <item>New York</item>
        <item>Colorado</item>
    </string-array>
    <string name="radio1">Canadian</string>    
    <string name="radio2">US</string> 

xml

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.view.View;

public class CanProvselect extends Activity implements OnCheckedChangeListener {
    /** Called when the activity is first created. */
    private RadioGroup RadioProvs;
    private Button btn1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.canprovselect);

        btn1 = (Button)findViewById(R.id.button1); 
        RadioProvs = (RadioGroup) findViewById(R.id.rgProvs);
        RadioProvs.setOnCheckedChangeListener(this);
    }
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        ArrayAdapter<CharSequence> adapter = null;
        final Spinner spinner = (Spinner) findViewById(R.id.spinner);

        switch (checkedId) {
        case R.id.radio_1 :
            adapter = ArrayAdapter.createFromResource(
                    this, R.array.prov_array, android.R.layout.simple_spinner_item);     
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new OnItemSelected(
                    ));
            break;
        case R.id.radio_2 :
            adapter = ArrayAdapter.createFromResource(
                    this, R.array.prov1_array, android.R.layout.simple_spinner_item);     
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new OnItemSelected(
                    ));
            break;

        }
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)  {
            String selected = (String) spinner.getSelectedItem();
        Intent myIntent = new Intent (spinner.getSelectedItem(), ProvBC.class);
        startActivityForResult(myIntent, 0);    

}

        });
    }
}



onItemSelected

    import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class OnItemSelected implements OnItemSelectedListener {
    private boolean isFirst = true;

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

        if (isFirst) { 
            isFirst = false;
            } else {
        Toast.makeText(arg0.getContext(), "The country is"+arg0.getItemAtPosition(arg2).toString(), 
                Toast.LENGTH_SHORT).show();
            }
    }

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

    }

}

3 个答案:

答案 0 :(得分:1)

尝试以下

spinner.getSelectedItemId() OR spinner.getSelectedItem()

答案 1 :(得分:0)

修改 你需要这样做:

  btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)  {
                }
String selected = spinner.getSelectedItem().toString();


        });

并将意图传递给下一个活动,并将所选项目作为额外的

答案 2 :(得分:0)

感谢大家的帮助。搞定了。以下是其他人需要的示例。

btn1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Spinner sp =    (Spinner)findViewById(R.id.spinner);
            String spinnerString = null;
            spinnerString = sp.getSelectedItem().toString();

            if (spinnerString.equalsIgnoreCase("British Columbia")){
                Intent myIntent = new Intent (v.getContext(), ProvBC.class);
                startActivityForResult(myIntent, 0);
            } else 
                if (spinnerString.equalsIgnoreCase("Alberta")){
                    Intent myIntent = new Intent (v.getContext(), ProvAb.class);
                    startActivityForResult(myIntent, 0);
                } 

        }
    });