Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

android - 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 TextView. 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:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...