使用Textwatch计算

时间:2014-01-20 11:54:05

标签: java android

我要问的问题可能有点复杂。 请原谅我,如果我不能清楚地解释它。 这是我第一次接触到Android编码。

我想做一个做android的计算。 这是关于燃料日志。 我现在想做的是 当我保持价格(edittext)和抽水(edittext)时, 输出将立即显示在我放置的费用(textview)中。

我想这样做, 但我认为我没有为它做正确的编码。 我试过自己, 但我没有运行该应用程序,因为我认为我所做的代码没有完全完成/正确。

我很想听听你们的建议。 有人可以帮我解决一下吗?

谢谢你, 你的帮助将非常感激。 (:

MainActivity.java

  public class MainActivity extends Activity {

    Button saveButton = null;
    EditText dateEdit; 
    EditText priceEdit;
    EditText pumpEdit;
    TextView costView;
    EditText odometerEdit;
    TextView fconView;




    public boolean isNumeric(String str)
    {
        return str.matches("-?\\d+(\\.\\d+)?"); 
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




        setContentView(R.layout.activity_main);
        Bundle bundle = getIntent().getExtras();
        float price = Float.parseFloat(bundle.getString("priceEdit"));
        float pump = Float.parseFloat(bundle.getString("pumpEdit"));

        costView = (TextView)findViewById(R.id.tcost);
        dateEdit = (EditText)findViewById(R.id.date);
        priceEdit = (EditText)findViewById(R.id.fuelprice);
        pumpEdit = (EditText)findViewById(R.id.fuelpump);




          TextWatcher   mCostChangeWatcher = new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub

                }

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

                }

                @Override
                public void afterTextChanged(Editable s) {
                                if(!TextUtils.isEmpty(priceEdit.getText()) && !TextUtils.isEmpty(pumpEdit.getText())){
                                    costView.setText(calculateCost(priceEdit.getText(), pumpEdit.getText()));
                    }

                }

                private float calculateCost(float price, float pump) {

                    final float costCal = (price * pump);

                    return costCal;
                    // TODO Auto-generated method stub



                }
            };

            priceEdit.addTextChangedListener(mCostChangeWatcher); 
            pumpEdit.addTextChangedListener(mCostChangeWatcher); 



        saveButton = (Button) findViewById(R.id.saveBTN);
        saveButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
                try
                {
                    dbAdaptor.open();
                    String date = dateEdit.getText().toString();
                    String price = priceEdit.getText().toString();
                    String pump = pumpEdit.getText().toString();
                     String cost = Float.toString();
                    String odometer = odometerEdit.getText().toString();
                    String fcon = fconView.getText().toString();
                    dbAdaptor.insertLog(date, price, pump, cost, odometer, fcon);

                }
                catch(Exception e){
                    Log.d("Fuel Log", e.getMessage());
                }
                finally
                {
                    if(dbAdaptor != null)
                        dbAdaptor.close();
                }
            }
        });

    }//oncreate


}//main

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
     android:orientation="vertical"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >


   <TableLayout 
        android:id="@+id/tableLayout1" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:stretchColumns="1">

        <TableRow 
            android:id="@+id/tableRow1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
             <TextView 
                android:id="@+id/datetxtview"
                android:text="@string/date"  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>
              <EditText
                android:id="@+id/date" 
                android:text=""
                android:inputType="date" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </EditText>
       </TableRow>

         <TableRow 
            android:id="@+id/tableRow2" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView 
                android:id="@+id/fuelpricetxtview" 
                android:text="@string/fuelprice"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>
            <EditText
                android:id="@+id/fuelprice" 
                android:text="" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </EditText>
        </TableRow>
         <TableRow 
            android:id="@+id/tableRow3" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView 
                android:id="@+id/fuelpumptxtview" 
                android:text="@string/fuelpump"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>
            <EditText
                android:id="@+id/fuelpump" 
                android:text="" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </EditText>
        </TableRow>


          <TableRow 
            android:id="@+id/tableRow4" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView 
                android:id="@+id/totalcosttxtview" 
                android:text="@string/totalcost"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>

            <TextView
                android:id="@+id/tcost"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />

        </TableRow>



         <TableRow 
            android:id="@+id/tableRow5" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView 
                android:id="@+id/odometertxtview" 
                android:text="@string/odometer"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>

            <EditText
                android:id="@+id/odometer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10" >

                <requestFocus />
            </EditText>

        </TableRow>   
         <TableRow 
            android:id="@+id/tableRow6" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView 
                android:id="@+id/fctxtview" 
                android:text="@string/fc"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>

            <TextView
                android:id="@+id/fcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />

        </TableRow>     
        </TableLayout>


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/saveBTN"
            android:text="@string/save"
            android:layout_width="wrap_content"
            android:layout_height="60px" >
        </Button>

        <Button
            android:id="@+id/cancelBTN"
            android:text="@string/cancel"
            android:layout_width="wrap_content"
            android:layout_height="60px" >
        </Button>
    </LinearLayout>



</LinearLayout>

1 个答案:

答案 0 :(得分:0)

为两个编辑文本添加更改侦听器

    priceEdit.addTextChangedListener(mCostChangeWatcher);
    pumpEdit.addTextChangedListener(mCostChangeWatcher);


    TextWatcher mCostChangeWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

// TODO Auto-generated method stub

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
                        if(!TextUtils.isEmpty(priceEdit.getText()) && !TextUtils.isEmpty(pumpEdit.getText())){
                costEdit.setText(calculateCost(priceEdit.getText(), pumpEdit.getText()));
            }

        }
    };
相关问题