4

数値のリストを設定したスピナー「絞り」と、2 つのオプションを含むスピナー「モード」があります。ボタンを押すと、「絞り」からの現在の選択や「モード」から派生した値など、さまざまな入力を使用して計算を実行する必要があります。計算で使用できるように、スピナーの値を呼び出すにはどうすればよいですか?

また、スピナーモードの選択を使用して、計算に実装する前にこの他の値を設定するにはどうすればよいですか? より具体的には、スピナーが 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); 
}
}

これは実際の式ではなく、すべてが正しく接続されていることを確認するためのテストです。スピナーの「絞り」と小/大スピナーの「モード」の値をどのように呼びますか

4

3 に答える 3

5

スピナーの値を取得するには、次のいずれかを適切に呼び出します。

Object item = spinner.getSelectedItem();

long id = spinner.getSelectedItemId();

int position = getSelectedItemPosition();

View selectedView = getSelectedView();

あなたの場合、次のように、スピナーを最終として宣言し、選択したアイテムを 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 でスコープがどのように機能するかを学ぶことは、おそらくあなたにとって良いことでしょう。

于 2010-01-25T03:18:51.047 に答える
3

プログラムで ArrayAdapter に既知の型のオブジェクトを入力して、それを使用してみませんか。これを行う同様の性質のチュートリアル (下部のリンク) を作成しました。基本的な前提は、Java オブジェクトの配列を作成し、スピナーにそのことを伝え、それらのオブジェクトをスピナー クラスから直接使用することです。私の例では、次のように定義された「状態」を表すオブジェクトがあります。

パッケージ com.katr.spinnerdemo;

パブリッククラス状態{

// 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

于 2010-10-30T15:16:21.760 に答える
0

R.array.apertures が文字列配列の場合、次を使用できます

Object item = spinner.getSelectedItem(); 

if(item != null) {

String SelectedString = item.toString();

}

他のオブジェクトの場合は、Person と言います。

それから

if(item != null) {

Person SelectedString = (Person) item;

}
于 2010-10-20T18:07:31.990 に答える