调用Spinner的值?同时,根据Spinner的值使用其他值。

4
我有一个名为“光圈”的下拉菜单,其中包含一系列数字,还有一个名为“模式”的下拉菜单,其中有两个选项。当按下按钮时,我需要运行计算,使用各种输入,包括来自“光圈”的当前选择和从“模式”派生的值。如何调用下拉菜单的值,以便我可以在计算中使用它?
此外,在实现计算之前,如何使用“模式”下拉菜单的选择来设置另一个值?具体而言,如果将下拉菜单设置为 Small,则我需要使用0.015进行计算,而如果选择Large,则需要使用0.028。
我的其他输入是EditText视图,因此我现在的设置如下:
    input = (EditText) findViewById(R.id.input1); 
    input2 = (EditText) findViewById(R.id.input2);
    input3 = (EditText) findViewById(R.id.input3); 
    input4 = (EditText) findViewById(R.id.input4);
    output = (TextView) findViewById(R.id.result); 
    output2 = (TextView) findViewById(R.id.result2);

    //aperture dropdown
    Spinner s = (Spinner) findViewById(R.id.apt);
    ArrayAdapter adapter2 = ArrayAdapter.createFromResource(        this, R.array.apertures,       
    android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter2);

    //button
    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {             
     public void onClick(View v) {                 
      // Perform action on click
      doCalculation();
       }         
      });
     }

private void doCalculation() { 
    // Get entered input value 
    String strValue = input.getText().toString(); 
    String strValue2 = input2.getText().toString();
    String strValue3 = input3.getText().toString(); 
    String strValue4 = input4.getText().toString();

    // Perform a hard-coded calculation 
    double imperial1 = (Double.parseDouble(strValue3) + (Double.parseDouble(strValue4) / 12));
    double number = Integer.parseInt(strValue) * 2; 
    double number2 = ((number / 20) + 3) / Integer.parseInt(strValue2);

    // Update the UI with the result 
    output.setText("Result:  "+ number); 
    output2.setText("Result2: "+ imperial1); 
}
}

这不是实际的方程式,只是一个测试,以确保一切连接正常。我该如何调用微调器“光圈”的值和小/大微调器“模式”的值?

3个回答

5

要获取微调框的值,请根据情况调用以下其中之一:

Object item = spinner.getSelectedItem();

long id = spinner.getSelectedItemId();

int position = getSelectedItemPosition();

View selectedView = getSelectedView();

在您的情况下,您可以将spinner声明为final,并将所选项目传递到doCalculations()方法中,如下所示:
    final Spinner aptSpinner = (Spinner) findViewById(R.id.apt);
    ...

    //button
    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {             
     public void onClick(View v) {                 
      // Perform action on click
      doCalculation(aptSpinner.getSelectedItem());
       }         
      });
     }

    private void doCalculation(Object selectedItem) { 

        ...

    }

您最好选择一本基础的Java书籍,学习Java中的作用域是如何工作的。


谢谢回复。我对Java非常陌生,所以请原谅。我相信 Object item = spinner.getSelectedItem(); 是正确的,但是我该如何实现它呢?当我尝试从计算内部调用它时,Eclipse会说无法解析“aperture”。如果我将代码放在计算外面,它可以正常工作,但是在计算内部它无法解析对象名称(在您的示例中为“item”)。 - Sean
谢谢您的更新。Eclipse没有显示任何错误,但是当我在计算中有“Object item = aperture.getSelectedItem();”(旋转器命名为aperture,而不是您示例中的aptSpinner)时,当我点击按钮时会导致应用程序崩溃。这可能是什么原因呢?再次抱歉问了这么多问题。我知道我应该先学习Java,而不是像这样头脑发热地跳进去,但我需要这个应用程序来拍摄电影,这个周末就要拍了。不用说,我很快就会得到一些Java书籍 :) - Sean

3
为什么不使用已知类型的对象通过编程方式填充ArrayAdapter并使用它呢?我已经写了一个类似的教程(底部链接),介绍如何做到这一点。基本思路是创建一个Java对象数组,告诉Spinner相关信息,然后直接从Spinner类中使用这些对象。在我的示例中,我有一个表示“州”的对象,其定义如下:

package com.katr.spinnerdemo;

public class State {

// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";

// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
    id = _id;
    name = _name;
    abbrev = _abbrev;
}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control.  If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
    return( name + " (" + abbrev + ")" );
}
}

然后,您可以按照以下方式使用这些类的数组填充下拉列表:
       // Step 1: Locate our spinner control and save it to the class for convenience
    //         You could get it every time, I'm just being lazy...   :-)
    spinner = (Spinner)this.findViewById(R.id.Spinner01);

    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
          android.R.layout.simple_spinner_item, new State[] {   
                new State( 1, "Minnesota", "MN" ), 
                new State( 99, "Wisconsin", "WI" ), 
                new State( 53, "Utah", "UT" ), 
                new State( 153, "Texas", "TX" ) 
                });

    // Step 3: Tell the spinner about our adapter
    spinner.setAdapter(spinnerArrayAdapter);  

您可以按照以下方式检索所选项目:
State st = (State)spinner.getSelectedItem();

现在您拥有了一个可用的Java类。如果您想要在下拉菜单的值发生变化时进行拦截,只需实现OnItemSelectedListener并添加适当的方法来处理事件即可。

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
    // Get the currently selected State object from the spinner
    State st = (State)spinner.getSelectedItem();

    // Now do something with it.
} 

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

您可以在此处找到完整的教程: http://www.katr.com/article_android_spinner01.php

0
如果R.array.apertures是一个字符串数组,那么你可以使用:
Object item = spinner.getSelectedItem(); 

if(item != null) {

String SelectedString = item.toString();

}

如果是其他的对象,比如人。

那么

if(item != null) {

Person SelectedString = (Person) item;

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接