在一个EditText中输入的数据必须同时显示在其他EditText中

时间:2013-10-14 05:00:17

标签: java android

例如。

空是第一个EditText,total_empty是另一个编辑文本 如果我在空的edittext中输入1意味着它必须同时在total_empty edittext上显示为1。

之后我有一个名为new.i的EditText将在EditText new中给出3作为值。

之后我必须添加上面三个EditText值(1 + 2 + 3)并在另一个名为fill的EditText中显示总和。

Atlast我还有另一个名为total_fill的editText,我必须在上面的“fill”edittext中显示相同的值。

我的代码在下面给出

的EditText

empty_cyl_recvd,new_cyl_recvd,filled_cyl_unload,dmg_cyl_recvd,total_filled_cyl,total_dmg_cyl,total_em
pty_cyl;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    empty_cyl_recvd = (EditText) findViewById(R.id.empty_cyl_recvd);
    empty_cyl_recvd.setInputType(InputType.TYPE_CLASS_NUMBER);
    empty_cyl_recvd.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

 new_cyl_recvd = (EditText) findViewById(R.id.new_cyl_recvd);
 new_cyl_recvd.setInputType(InputType.TYPE_CLASS_NUMBER);
       new_cyl_recvd.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

 filled_cyl_unload = (EditText) findViewById(R.id.filled_cyl_unload);
 filled_cyl_unload.setInputType(InputType.TYPE_CLASS_NUMBER);
 filled_cyl_unload.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

 dmg_cyl_recvd = (EditText) findViewById(R.id.dmg_cyl_recvd);
 dmg_cyl_recvd.setInputType(InputType.TYPE_CLASS_NUMBER); 
 dmg_cyl_recvd.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

     total_dmg_cyl=(EditText)findViewById(R.id.sdff);
 total_empty_cyl=(EditText)findViewById(R.id.wertyu);
 total_filled_cyl=(EditText)findViewById(R.id.gfhgftg);


    empty_cyl_recvd.addTextChangedListener(new TextWatcher()
     {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

int a=Integer.parseInt(empty_cyl_recvd.getText().toString());
int b=Integer.parseInt(Util.EMPTY_LIST.get(0).toString());
int c=a+b;
total_empty_cyl.setText(""+c);
}});

filled_cyl_unload.addTextChangedListener(new TextWatcher()
     {

        @Override
        public void afterTextChanged(Editable s) {
            // 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 onTextChanged(CharSequence s, int start, int before,
                int count) {

        int a=Integer.parseInt(filled_cyl_unload.getText().toString());
        int b=Integer.parseInt(Util.FILL_LIST.get(0).toString());
        int c=a+b;
        total_filled_cyl.setText(""+c);
             }

     });
     dmg_cyl_recvd.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
            // 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 onTextChanged(CharSequence s, int start, int before,
                int count) {

            int a=Integer.parseInt(dmg_cyl_recvd.getText().toString());
            int b=Integer.parseInt(Util.DAMAGE_LIST.get(0).toString());
            int c=a+b;

            total_dmg_cyl.setText(""+c);
}

     });

1 个答案:

答案 0 :(得分:0)

// try this

**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_height="match_parent">

    <EditText
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:inputType="numberDecimal"
            android:hint="new"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:inputType="numberDecimal"
            android:hint="empty"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/total_empty"
            android:layout_width="match_parent"
            android:inputType="numberDecimal"
            android:layout_height="wrap_content"
            android:hint="total empty"
            android:editable="false"/>

    <EditText
            android:id="@+id/fill"
            android:layout_width="match_parent"
            android:inputType="numberDecimal"
            android:layout_height="wrap_content"
            android:hint="fill"
            android:editable="false"/>

    <EditText
            android:id="@+id/total_fill"
            android:layout_width="match_parent"
            android:inputType="numberDecimal"
            android:hint="total fill"
            android:layout_height="wrap_content"
            android:editable="false"/>

</LinearLayout>

**MainActivity**

package com.example.MyTest;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class MainActivity extends Activity {

   private EditText first;
   private EditText empty;
   private EditText total_empty;
   private EditText fill;
   private EditText total_fill;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        first = (EditText) findViewById(R.id.first);
        empty = (EditText) findViewById(R.id.empty);
        total_empty = (EditText) findViewById(R.id.total_empty);
        fill = (EditText) findViewById(R.id.fill);
        total_fill = (EditText) findViewById(R.id.total_fill);

        first.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.toString().trim().length()>0){
                    if(empty.getText().toString().trim().length()>0){
                        total_empty.setText(String.valueOf(((int)(Double.valueOf(empty.getText().toString().trim())+Double.valueOf(editable.toString().trim())))));
                    }else{
                        total_empty.setText(String.valueOf(editable.toString().trim()));
                    }
                    fill.setText(total_empty.getText().toString());
                    total_fill.setText(total_empty.getText().toString());
                }else{
                    if(empty.getText().toString().trim().length()>0){
                        total_empty.setText(empty.getText().toString());
                        fill.setText(empty.getText().toString());
                        total_fill.setText(empty.getText().toString());
                    }else{
                        total_empty.setText("");
                        fill.setText("");
                        total_fill.setText("");
                    }
                }
            }
        });

        empty.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.toString().trim().length()>0){
                    if(first.getText().toString().trim().length()>0){
                        total_empty.setText(String.valueOf(((int)(Double.valueOf(editable.toString().trim())+Double.valueOf(first.getText().toString().trim())))));
                    }else{
                        total_empty.setText(String.valueOf(editable.toString().trim()));
                    }
                    fill.setText(total_empty.getText().toString());
                    total_fill.setText(total_empty.getText().toString());
                }else{
                    if(first.getText().toString().trim().length()>0){
                        total_empty.setText(first.getText().toString());
                        fill.setText(first.getText().toString());
                        total_fill.setText(first.getText().toString());
                    }else{
                        total_empty.setText("");
                        fill.setText("");
                        total_fill.setText("");
                    }
                }
            }
        });
    }

}