如何使用编辑文本计算多个微调器值?

时间:2014-05-25 05:43:14

标签: android spinner

我是android编程的新手,我希望使用具有值字符串和编辑文本的多个微调器进行计算。和按钮单击按钮并在文本视图上显示。请帮我解决,以便我可以学习下一个主题。

THx的

我的活动.java代码:

public class MainActivity extends Activity {
private EditText jumlah;

String[] spinnerValues = { "Bakso", "Es Buah" };

String[] spinnerSubs = {"10000", "8000" };

int total_images[] = { R.drawable.bakso, R.drawable.es_buah };

private Button button1;

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

    Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
    mySpinner.setAdapter(new MyAdapter(this, R.layout.menu_resto,
            spinnerValues));
    jumlah = (EditText) findViewById(R.id.editText1);
    button1 = (Button) findViewById(R.id.button1);

    initButton();
}



private void initButton() {
    button1.setOnClickListener(new OnClickListener() {
        // this one performs an action when our button is clicked. it performs whatever is below

        @Override
        public void onClick(View v) {
          //  String strA = i want call the spinersubs value that chsoed. how ?
            String strB = jumlah.getText().toString();


            Double dblAnswer = doCalc(strA, strB);              

            TextView lblAnswer = (TextView) findViewById(R.id.lblAnswer);

            // the disadvantage is that we can't do anything to it outside of this curly 
            // in general it's wasteful to use fields when you can suffice with local variable
            String answer = String.valueOf(dblAnswer);
            // we get our answer and turn it to a string.
            lblAnswer.setText(answer);
            // finally we set our result to the textView.
        }
    });
}
  public double doCalc(String a, String b) {

  double dblA = Double.parseDouble(a);
  double dblB = Double.parseDouble(b);
  return dblA * dblB;
    }
 class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
        super(ctx, txtViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
        return getCustomView(position, cnvtView, prnt);
    }
    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt) {
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView,
            ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View mySpinner = inflater.inflate(R.layout.menu_resto, parent,
                false);
        TextView main_text = (TextView) mySpinner
                .findViewById(R.id.text_main_seen);
        main_text.setText(spinnerValues[position]);

        TextView subSpinner = (TextView) mySpinner
                .findViewById(R.id.sub_text_seen);
        subSpinner.setText(spinnerSubs[position]);

        ImageView left_icon = (ImageView) mySpinner
                .findViewById(R.id.left_pic);
        left_icon.setImageResource(total_images[position]);

        return mySpinner;
    }

这是我的XML

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="X - Resto Menu"
    android:textSize="30px" />

<Spinner
    android:id="@+id/spinner_show"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100px"
    android:drawSelectorOnTop="true" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner_show"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:ems="10" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner_show"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:text="Jumlah Pesanan" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:onClick="@string/hitung"
    android:text="@string/pesan" />

<TextView
    android:id="@+id/lblAnswer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:text="" />

1 个答案:

答案 0 :(得分:1)

您需要为CustomOnItemSelectedListener

实施Spinner
public class CustomOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(), 
    "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
    Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}

}

然后将此Listener设置为您的Spinner

mySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());

并在Button点击中,您将获得Spinner选择的值,例如

  btnSubmit.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    Toast.makeText(MyAndroidAppActivity.this,
    "OnClickListener : " + 
            "\nSpinner : "+ String.valueOf(mySpinner.getSelectedItem()) 
        ,Toast.LENGTH_SHORT).show();
  }

});

转到此Tutorial