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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…