Automatic calculation
I'm making an app where i want to automatically make an calculation.
the numbers 1 and 2 are EditText, number 3 is a T开发者_StackOverflow中文版extView. when i fill in the numbers 1 and 2, i want it to automatically sum up and show a result in the TextView. in this example 1+2=3
How to set up an activity for this
i made an xml example:
Here is add_two_numbers.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="First Number : "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Second Number : "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Result"
android:id="@+id/textView_result"
android:textColor="#FF00FF"
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
And here is an Activity AddTwoNumbers.java
public class AddTwoNumbers extends Activity {
EditText editText1;
EditText editText2;
TextView textViewResult;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.add_two_numbers);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
textViewResult = (TextView) findViewById(R.id.textView_result);
editText1.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText2.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private String addNumbers() {
int number1;
int number2;
if(editText1.getText().toString() != "" && editText1.getText().length() > 0) {
number1 = Integer.parseInt(editText1.getText().toString());
} else {
number1 = 0;
}
if(editText2.getText().toString() != "" && editText2.getText().length() > 0) {
number2 = Integer.parseInt(editText2.getText().toString());
} else {
number2 = 0;
}
return Integer.toString(number1 + number2);
}
}
And I have tested it on 2.3 platform. It's working fine.
You may need to provide a key listener to each of your EditTexts, such that after each key event you can refresh your values for the fields and present a new calculation.
You need to add two TextViews and register a text changed listener for them. whenever the text changes, you can validate input, do the calculation and update the display.
If u want to raise any event automatically,you need to add the appropriate listeners to that view component
edittext1.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(
Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(
CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(
CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
});
edittext2.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(
Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(
CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(
CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
});
精彩评论