首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用微调函数的值?此外,还可以根据微调器的值使用其他值

调用微调函数的值?此外,还可以根据微调器的值使用其他值
EN

Stack Overflow用户
提问于 2010-01-25 10:35:02
回答 3查看 9.4K关注 0票数 4

我有一个带有数字列表的微调“光圈”和一个带有两个选项的微调“模式”。当按钮被按下时,我需要使用各种输入来运行计算,包括从“光圈”中选择的当前值和从“模式”中派生的值。如何调用微调器的值,以便在计算中使用它?

另外,我如何使用微调器模式的选择来设置另一个值,然后在计算中实现它?更具体地说,如果微调器设置为小,那么我在计算中使用的值是0.015,而如果选择了大,我需要使用0.028

我的其他输入是EditText视图,所以现在我的设置如下:

代码语言:javascript
复制
    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); 
}
}

这不是实际的等式,它只是一个测试,以确保一切都正确连接。我如何调用微调器的值‘光圈’和小/大微调器‘模式’

EN

回答 3

Stack Overflow用户

发布于 2010-01-25 11:18:51

要获取微调器的值,请根据需要调用以下方法之一:

代码语言:javascript
复制
Object item = spinner.getSelectedItem();

long id = spinner.getSelectedItemId();

int position = getSelectedItemPosition();

View selectedView = getSelectedView();

在您的示例中,可以将微调器声明为final,并将选定的项传递给doCalculations()方法,如下所示:

代码语言:javascript
复制
    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中的scope是如何工作的可能会很有帮助。

票数 5
EN

Stack Overflow用户

发布于 2010-10-30 23:16:22

为什么不用已知类型的对象以编程方式填充ArrayAdapter并使用它呢?我已经写了一个类似性质的教程(底部的链接)来做这件事。基本前提是创建一个Java对象数组,告诉微调器有关,然后直接从微调器类使用这些对象。在我的示例中,我有一个表示"State“的对象,它的定义如下:

打包com.katr.spinnerdemo;

公共类状态{

代码语言:javascript
复制
// 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 + ")" );
}
}

然后,您可以使用这些类的数组填充微调器,如下所示:

代码语言:javascript
复制
       // 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);  

您可以按如下方式检索所选项目:

代码语言:javascript
复制
State st = (State)spinner.getSelectedItem();

现在,您可以使用一个经过优化的java类了。如果您想在微调器值发生变化时进行拦截,只需实现OnItemSelectedListener并添加适当的方法来处理事件即可。

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2010-10-21 02:07:32

如果R.array.apertures是字符串数组,则可以使用

代码语言:javascript
复制
Object item = spinner.getSelectedItem(); 

if(item != null) {

String SelectedString = item.toString();

}

如果是任何其他对象,就说人。

然后

代码语言:javascript
复制
if(item != null) {

Person SelectedString = (Person) item;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2129903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档