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

时间:2012-03-11 16:55:31

标签: android text spinner edit

我试图建立一个简单的订单系统。因此,用户从微调器中选择汉堡类型,然后用户可以输入数量,然后在后台进行一些数学运算,在底部设置总价格的文本视图。我不确定我使用的方法是什么,以便在用户更改文本视图数量时动态更新。

如果我只是说所有汉堡都是2.50,我需要某种方法说,如果/当数据输入到编辑文本中时,取出该数据然后将其计时2.50并将其设置在下面的全文本视图中。我只是不确定它的方法部分,因此它是动态的,并且会在编辑文本数量发生变化时发生变化。

[代码]

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) {
            }
        });     





    }



}

[/代码]

1 个答案:

答案 0 :(得分:1)

您必须为TextWatcher实施EditText。首先创建一个数组,它将保存汉堡的价格(从微调器映射汉堡类型)和一个int字段,它将保留旋转器中的当前选择:

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

然后为TextWatcher

实施EditText
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
            }

同样在您的微调器侦听器中设置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);
                    }
                }
            });

注意:您必须注意从EditText解析值时可能出现的异常(例如,如果用户输入值,然后删除所有数字,您将有异常因为应用程序将尝试解析空字符串)