问题
In an Android app, I'm using two EditText
controls and multiplying their two values.
If one EditText
is null
and in the second one I put a value, it's not working properly.
How can I deal with this case, in which I have a value in one EditText
and a null
in the other and I want to multiply the two values?
回答1:
First of all, you need to have a trigger for when to perform the calculation. Say it's a button, or, even better, every time the value of one of your EditText
s changes:
private EditText editText1,
editText2;
private TextView resultsText;
...............................
// Obtains references to your components, assumes you have them defined
// within your Activity's layout file
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
resultsText = (TextView)findViewById(R.id.resultsText);
// Instantiates a TextWatcher, to observe your EditTexts' value changes
// and trigger the result calculation
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
calculateResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
// Adds the TextWatcher as TextChangedListener to both EditTexts
editText1.addTextChangedListener(textWatcher);
editText2.addTextChangedListener(textWatcher);
.....................................
// The function called to calculate and display the result of the multiplication
private void calculateResult() throws NumberFormatException {
// Gets the two EditText controls' Editable values
Editable editableValue1 = editText1.getText(),
editableValue2 = editText2.getText();
// Initializes the double values and result
double value1 = 0.0,
value2 = 0.0,
result;
// If the Editable values are not null, obtains their double values by parsing
if (editableValue1 != null)
value1 = Double.parseDouble(editableValue1.toString());
if (editableValue2 != null)
value2 = Double.parseDouble(editableValue2.toString());
// Calculates the result
result = value1 * value2;
// Displays the calculated result
resultsText.setText(result.toString());
}
回答2:
On button click it multiplies 2 values of the EditText
boxes and checks wether they are empty or not:
private EditText txtOne;
private EditText txtTwo;
private EditText txtResult;
private Button btnCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOne = (EditText) findViewById(R.id.editText1);
txtTwo = (EditText) findViewById(R.id.editText2);
txtResult = (EditText) findViewById(R.id.editText3);
btnCount = (Button) findViewById(R.id.button1);
btnCount.setText("Count");
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (txtOne.getText() != null || txtTwo.getText() != null)
txtResult.setText(String.valueOf(Double.parseDouble(txtOne.getText().toString()) * Double.parseDouble(txtTwo.getText().toString())));
}
});
}
回答3:
Slight variation of @luvieere code:
//import all the importables
public class MainActivity extends ActionBarActivity
{
EditText Text1,
Text2;
double value1,
value2,
result;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ResultField = (TextView)findViewById(R.id.ResultField);
TextWatcher textWatcher = new TextWatcher(){
public void afterTextChanged(Editable s){
calcResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
Text1 = (EditText)findViewById(R.id.Text1);
Text2 = (EditText)findViewById(R.id.Text2);
Text1.addTextChangedListener(textWatcher);
Text2.addTextChangedListener(textWatcher);
}
private void calcResult() throws NumberFormatException {
Editable editableText1 = Text1.getText(),
editableText2 = Text2.getText();
double value1 = 0.0,
value2 = 0.0,
result;
// NOTE: “&& editableText1.length() >= 1” prevents possible “invalid Double” errors!
if (editableText1 != null && editableText1.length() >= 1)
value1 = Double.parseDouble(editableText1.toString());
if (editableText2 != null && editableText2.length() >= 1)
value2 = Double.parseDouble(editableText2.toString());
// Whatever your magic formula is
result = value1 * value2;
ResultField.setText(Double.toString(result));
{// Displays result to 2 decimal places
ResultField.setText(String.format("%1.2f", result));
}
}
}
来源:https://stackoverflow.com/questions/6965063/how-to-calculate-edittext-value-in-android