如何在Spinner中使用开关盒?

时间:2012-10-17 13:15:17

标签: android spinner switch-statement

我正在尝试弄清楚如何使用switch-case base打印一个值,而不是用户选择的内容。我可以轻松地打印用户在微调器中选择的内容,但我需要与之相关的值。我希望我的代码中只缺少一些小东西。

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Test extends Activity implements OnClickListener {
    EditText input1;
    Spinner answer;
    Object value;
    int yourInt = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        input1 = (EditText) findViewById (R.id.editText1);
        answer = (Spinner) findViewById (R.id.spinner1);
        ((Button) findViewById (R.id.button1)).setOnClickListener(this);
        answer.setOnItemSelectedListener(new OnItemSelectedListener() {


            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


                value = parent.getItemAtPosition(position);
                switch (position) {
                case 0:
                    yourInt = 1;
                    break;

                case 1:
                    yourInt = 2;
                    break;
                }

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

            }
        });
    }
    public void onClick (View v) {

        input1.setText(yourInt);
    }
}

1 个答案:

答案 0 :(得分:2)

switch (position) {
            case 0:
                yourInt = 1;
                input1.setText(""+yourInt);
                break;

            case 1:
                yourInt = 2;
                input1.setText(""+yourInt);
                break;
            }