在Android應用程序中,我使用兩個EditText
控件並將它們的兩個值相乘。 如果其中一個EditText
是null
,並且在第二個中我提出了一個值,它不能正常工作。 我該如何處理這種情況,其中我在另一個EditText
和null
中有一個值,我想將這兩個值相乘?如何計算Android中的EditText值?
1
A
回答
5
首先,你需要有關於何時執行計算的觸發器。說這是一個按鈕,或者,甚至更好,每一次你EditText
變遷之一的值:
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
在按鈕單擊它們是否是空的,或者不是相乘的EditText
箱2點的值,並檢查:
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())));
}
});
}
1
@luvieere代碼的輕微變化:
//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));
}
}
}
相關問題
- 1. 如何計算Edittext的整數值Android
- 2. Android EditText計算暫停
- 3. 如何在Android中的listview中自動計算edittext
- 4. Android - 如何從edittext中獲取文本並計算它們?
- 5. 如何檢查空的EditText計算
- 6. Android中的EditText值
- 7. 如何在Android中設計EditText和Button
- 8. 如何從android中的EditText獲取值?
- 9. 如何處理Android中的null EditText值?
- 10. 如何計算索引json的值android
- 11. (Android)如何抓取EditText值?
- 12. Android - 如何增加EditText值?
- 13. 如何在edittext上實現計算器?
- 14. editText的實時計算?
- 15. 如何計算計數值?
- 16. 獲取EditText中的值Android
- 17. 如何計算Android中Edittext字段的行數,字數和字符數
- 18. 計算Android的DeviceMotionEvent值
- 19. 檢測並計算EditText中的特殊字符 - Android
- 20. Android EditText值
- 21. android EditText值
- 22. On Edittext自動計算
- 23. 如何計算Android中的時間
- 24. R:如何計算計算字段列中的值R
- 25. 如何計算MS reportviewer/rdlc中的平均計算平均值?
- 26. 如何計算MySQL中的計算平均值?
- 27. 如何計算Android中的動態編輯文本值
- 28. 我如何讓Android Studio在textView中顯示計算的值
- 29. 如何計算文本框中的值?
- 30. 如何計算文件中的行值?
我認爲你需要看看[這](http://developer.android.com/reference/android/widget/EditText.html) –