如何在Android的第三个EditText中获得两个EditText的减法结果?

时间:2015-09-21 05:29:41

标签: java android

我是Android编程的新手。在制作程序时,我一直在寻找程序来自动填充第三个EditText中两个EditText小部件的减法结果,而不需要任何按钮。请帮我找到解决方案。

4 个答案:

答案 0 :(得分:0)

int firstvalue=FirstText.getText.Tostring();
int secondvalue=SecondText.getText.Tostring();
int Answer=firstvalue - secondvalue;
thirdvalue.setText(Answer);

希望它有效。如果出现任何问题,请告诉我

答案 1 :(得分:0)

您可以使用OnEditorAction侦听器方法编辑文本,并在焦点更改时执行您想要的操作

editText2.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
 if (actionId == EditorInfo.IME_ACTION_DONE) {
 //do here your stuff f
  int value1=Integer.valueOf(edittext1.getText().Tostring());
  int value2=Integer.valueOf(edittext2.getText().Tostring());
  int value3=value1 - value2;
  thirdvalue.setText(String.valueOf(value3));
 return true;
 }
 return false;
} });

答案 2 :(得分:0)

   EditText  e1=(EditText) findViewById(R.id.editText1);
   EditText  e2=(EditText) findViewById(R.id.editText2);
   EditText e3=(EditText)  findViewById(R.id.editText3);




e2.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
        int count) {
    if(!s.equals("") )
            {
   float a= Float.parseFloat(e1.getText().toString());
   float b= Float.parseFloat(e2.getText().toString());
   float c=a-b;
   e3.setText(String.valueOf(c);

           }
    }

}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {

}

public void afterTextChanged(Editable s) {

}
 });

答案 3 :(得分:0)

当然我假设您将完成所有错误处理。但是这个代码只有在两个字段都填满时才会显示!。

Field1 = (EditText)findViewById(R.id.field1);
Field2 = (EditText)findViewById(R.id.field2);
Field3 = (EditText)findViewById(R.id.field3);


Field1.addTextChangedListener(new TextWatcher() {


   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0 && Field2.getText().length() != 0)
        Field3.setText(""+(Integer.parseInt(s.toString()) - Integer.parseInt(Field2.getText().toString())));
   }
  });

Field2.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0 && Field1.getText().length() != 0)
         Field3.setText(""+( Integer.parseInt(Field1.getText().toString()) -  Integer.parseInt(s.toString())));
   }
  });