两个微调问题

时间:2017-11-12 10:40:55

标签: android spinner

我试图同时使用两个微调器,但无济于事。我创建了两个微调器并使用if语句来区分这两个。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner) findViewById(R.id.spinner);
    adapter = ArrayAdapter.createFromResource(this, R.array.om, 
    android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource
    (android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    adapter = ArrayAdapter.createFromResource(this, R.array.om2, 
    android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource
    (android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener
    ()

    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int 
        position, long id) {
            Spinner spinner = (Spinner) parent;
            if (spinner.getId() == R.id.spinner) {

                switch (position) {
                    case 0:
                        x = 1 / 1000;
                        break;
                    case 1:
                        x = 1;
                        break;
                    case 2:
                        x = 1000;
                        break;
                    case 3:
                        x = 1000000;
                        break;
                }
            }
            else if (spinner.getId() == R.id.spinner2) {

                switch (position) {
                    case 0:
                        y = 1 / 1000;
                        break;
                    case 1:
                        y = 1;
                        break;
                    case 2:
                        y = 1000;
                        break;
                    case 3:
                        y = 1000000;
                        break;
                }
            }
        }

Spinner和spinner2有两个数组,mΩ,Ω,kΩ和MΩ,x,y用于计算某些东西。计算结果(Float sum =((secondNumber y)/((secondNumber y)+(firstNumber * x)))* thirdNumber;)始终为0.计算使用一个微调器。我对java很新,想要一些帮助。 感谢

1 个答案:

答案 0 :(得分:0)

试试此代码

在活动中实施Lister,同时设置侦听器传递

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {


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


    spinner = (Spinner) findViewById(R.id.spinner);
    spinner2 = (Spinner) findViewById(R.id.spinner2);

    adapter = ArrayAdapter.createFromResource(this, R.array.om,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource
            (android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    adapter = ArrayAdapter.createFromResource(this, R.array.om2,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource
            (android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);

    // set spinner listener

    spinner.setOnItemSelectedListener(this);
    spinner2.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int
        position, long id) {
    Spinner spinner = (Spinner) parent;
    if (spinner.getId() == R.id.spinner) {

        switch (position) {
            case 0:
                x = 1 / 1000;
                break;
            case 1:
                x = 1;
                break;
            case 2:
                x = 1000;
                break;
            case 3:
                x = 1000000;
                break;
        }
    } else if (spinner.getId() == R.id.spinner2) {

        switch (position) {
            case 0:
                y = 1 / 1000;
                break;
            case 1:
                y = 1;
                break;
            case 2:
                y = 1000;
                break;
            case 3:
                y = 1000000;
                break;
        }
    }
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
}