简体   繁体   English

如何从编辑文本中获取数据并根据微调器值对其计时-动态更新?

[英]How to take data from an edit text and times it against a spinner value - dynamic update?

I'm trying to make a simple order system. 我正在尝试制作一个简单的订单系统。 So the user selects a burger type from a spinner, then the user can enter in the quantity and then there is some math taking place in the background which sets a text view at the bottom with the total price. 因此,用户从微调器中选择汉堡类型,然后用户可以输入数量,然后在后台进行一些数学运算,该数学运算将底部的文本视图设置为总价格。 I'm not sure what method I'm meant to use so that it is updated dynamically when the user changes the text view quantity. 我不确定要使用哪种方法,以便在用户更改文本视图数量时动态更新。

If i just say that all burgers are 2.50, i need some sort of method saying, if/when data is entered into the edit text , take that data and then times it by 2.50 and set it in the total text view below. 如果我只是说所有汉堡都是2.50,我需要某种方法说,如果/当在编辑文本中输入数据时,获取该数据,然后将其乘以2.50,并在下面的总文本视图中进行设置。 I'm just unsure on the method part of it so it's dynamic and will change when the edit text quantity is altered. 我只是不确定它的方法部分,所以它是动态的,并且在更改编辑文本数量时会改变。

[code] [码]

package placeorder.com;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Food extends Activity {

    String[] burgersarray, chipsarray, saladarray;
    Random order = new Random();
    double bquantity, hamburger, burgertotal;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food);

        EditText burgerquantity = (EditText) findViewById(R.id.et_burger);          
        TextView total = (TextView) findViewById(R.id.tv_total);
        Spinner burgers = (Spinner) findViewById(R.id.sp_burgers);      

        burgersarray = getResources().getStringArray(R.array.burgers);
        chipsarray = getResources().getStringArray(R.array.chips);
        saladarray = getResources().getStringArray(R.array.salad);      
        TextView orderid = (TextView) findViewById(R.id.tv_orderid);        

        int randomorder = order.nextInt(9999);
        orderid.setText("Order ID: " + randomorder);        


        ArrayAdapter<String> burgersadapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, burgersarray);

        burgers.setAdapter(burgersadapter);


        burgers.setOnItemSelectedListener(new OnItemSelectedListener()

        {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                String burger = (String) arg0.getSelectedItem();    
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });     





    }



}

[/code] [/码]

You'll have to implement a TextWatcher for your EditText . 您必须为EditText实现TextWatcher First make an array that will hold the prices of the burgers(mapping the burgers types from the spinner) and an int field that will hold the current selection from the spinner: 首先创建一个数组,该数组将保存汉堡的价格(映射来自微调框的汉堡类型)和一个int字段,将保留微调框的当前选择:

double[] prices = {1d, 2d, 3d};
int currentSelection = 0;

then implement the TextWatcher for your EditText : 然后为您的EditText实现TextWatcher

burgerquantity.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                double result = Integer.parseInt(s.toString())
                        * prices[currentSelection]; //obtain the total
                total.setText("" + result); //set the result
            }

Also in your spinner listener set the currentSelection field: 同样在您的微调监听器中,设置currentSelection字段:

    public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    currentSelection = arg2;
//this is needed because the user could enter a value in the EditText but change to another burger category and so you update the total for the new price
                    if (!burgerquantity.getText().toString().equals("")) {
                        double result = Integer.parseInt(burgerquantity.getText()
                                .toString()) * prices[currentSelection];
                        total.setText("" + result);
                    }
                }
            });

NOTE: You'll have to take care about the exception that could arise when parsing the value from the EditText (for example if the user enters a value and then deletes all the numbers you'll have an exception because the app will try to parse an empty string) 注意:您必须注意从EditText解析值时可能发生的异常(例如,如果用户输入一个值,然后删除所有数字,您将遇到一个异常,因为该应用将尝试解析一个空字符串)

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

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