调用微调器的值?此外,根据微调器的值使用其他值

Sea*_*ean 4 java android spinner

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

另外,在计算中如何使用微调器模式的选择来设置其他值?更具体地说,如果微调器设置为小,那么我在计算中使用的值是 0.015,而如果选择大我需要使用 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); 
}
}
Run Code Online (Sandbox Code Playgroud)

这不是实际的等式,它只是一个确保一切正常连接的测试。我如何称呼微调器“孔径”和小/大微调器“模式”的值

Jay*_*ren 5

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

Object item = spinner.getSelectedItem();

long id = spinner.getSelectedItemId();

int position = getSelectedItemPosition();

View selectedView = getSelectedView();
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您可以将微调器声明为 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) { 

        ...

    }
Run Code Online (Sandbox Code Playgroud)

拿起一本基本的 Java 书籍并了解 Java 中的作用域如何工作可能对您有好处。