简体   繁体   English

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

[英]How do calculate multiple spinner value with edit text?

Im new in android programing, I want make calculating with multiple spinner that have value string and edit text. 我是android编程的新手,我想使用多个具有值字符串和文本编辑器进行计算。 and proccess with button click and show on text view. 单击按钮并在文本视图上显示该过程。 please healp me to fix so i can learn next subject. 请帮助我修复,以便我可以学习下一个主题。

THx 谢谢

My activity .java code: 我的活动.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;
    }

Here my XML 这是我的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="" />

You need to implement CustomOnItemSelectedListener for Spinner like 您需要为Spinner实现CustomOnItemSelectedListener

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
}

}

and then set this Listener to your Spinner like 然后将此Listener设置为您的Spinner例如

mySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());

and in Button click you'll get a Spinner selected value like 然后在“ 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();
  }

});

Go to this for Tutorial 转到本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM