检查两个旋转器上选择的项目?

时间:2014-09-06 07:13:50

标签: java android select spinner

我的布局上有两个微调器,如下所示:

public class DemoActivity extends Activity {

    private static final String STATE_SCALE = "state-scale";
    private static final String STATE_CENTER_X = "state-center-x";
    private static final String STATE_CENTER_Y = "state-center-y";
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        String text1 = spinner1.getSelectedItem().toString();
        Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
        String text3 = spinner3.getSelectedItem().toString();
        if (text1.equals("Harris Academy"))
            Harris(spinner1);
        if (text1.equals("Harris Academy") && text3.equals("Ground Floor")) {
            Toast.makeText(getBaseContext(), text3, Toast.LENGTH_LONG).show();
            try {
                SubsamplingScaleImageView imageView =
                        (SubsamplingScaleImageView) findViewById(id.imageView);
                imageView.setImageAsset("DSC00277.png");

                if (savedInstanceState != null && 
                        savedInstanceState.containsKey(STATE_SCALE) &&
                        savedInstanceState.containsKey(STATE_CENTER_X) &&
                        savedInstanceState.containsKey(STATE_CENTER_Y)) {
                    imageView.setScaleAndCenter(savedInstanceState.getFloat(STATE_SCALE),
                            new PointF(savedInstanceState.getFloat(STATE_CENTER_X),
                            savedInstanceState.getFloat(STATE_CENTER_Y)));
                }
            } catch (IOException e) {
                Log.e(DemoActivity.class.getSimpleName(), "Could not load asset", e);
            }
        }
    }
}

当应用程序启动时,它可以正常工作,它获取微调器1和微调器3的值,然后根据值执行某些操作。

如何扩展它以便当我在任一个微调器上选择一个项目时,它会根据新值做一些事情?

2 个答案:

答案 0 :(得分:0)

要从用户选择来自微调器的项目

获取更新值

你必须实现微调器onItemSelectedListner

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
{
             public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {

         // Do your stuff here for spinner1

        }

        public void onNothingSelected(AdapterView<?> parent) {

        }

}); 
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
{
             public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {

         // Do your stuff here for spinner3

        }

        public void onNothingSelected(AdapterView<?> parent) {

        }

}); 

答案 1 :(得分:0)

如果您想在Spinner中操作选择项目的视图或数据,则需要实施 ItemSelectedListener Spinner

示例代码:

// in onCreate() or where you want to bind this Views with XML views
{
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner3 = (Spinner) findViewById(R.id.spinner3);
    // mItemSelectedListener Object of OnItemSelectedListener to handle Item Selection to Spinners.
    spinner1.setOnItemSelectedListener(mItemSelectedListener);
    spinner3.setOnItemSelectedListener(mItemSelectedListener);
}

// Creating an Object of Anonymous Class so no need to create another instance for same class
// OutSide of onCreate() method.
OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (view.getId()) {
        case R.id.spinner1:
              // Manipulates Views at selection of item in Spinner1
            break;
        case R.id.spinner3:
              // Manipulates Views at selection of item in Spinner3
            break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {}
};