Android开发简单计算器

界面如图

image

src/Ex03_16.java

package gphone.EX03_16;



import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;



public class EX03_16 extends Activity {

	EditText et1=null;

	EditText et2=null;

	Button btnJia=null;

	Button btnJian=null;

	Button btnCheng=null;

	Button btnChu=null;

	TextView tvFH=null;

	TextView tvResult=null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        et1=(EditText)findViewById(R.id.et1);

        et2=(EditText)findViewById(R.id.et2);

        btnJia=(Button)findViewById(R.id.btnJia);

        btnJian=(Button)findViewById(R.id.btnJian);

        btnCheng=(Button)findViewById(R.id.btnCheng);

        btnChu=(Button)findViewById(R.id.btnChu);

        tvFH=(TextView)findViewById(R.id.tvFH);

        tvResult=(TextView)findViewById(R.id.tvResult);

       //加

        btnJia.setOnClickListener(new Button.OnClickListener(){



			@Override

			public void onClick(View v) {

				// TODO Auto-generated method stub

				tvFH.setText(btnJia.getText());

			    tvResult.setText(Integer.toString(Integer.parseInt(et1.getText().toString())+Integer.parseInt(et2.getText().toString())));

			}

        	

        });

        //减

        btnJian.setOnClickListener(new Button.OnClickListener(){



			@Override

			public void onClick(View v) {

				// TODO Auto-generated method stub

				tvFH.setText(btnJian.getText());

			    tvResult.setText(Integer.toString(Integer.parseInt(et1.getText().toString())-Integer.parseInt(et2.getText().toString())));

			}

        	

        });

        //乘以

        btnCheng.setOnClickListener(new Button.OnClickListener(){



			@Override

			public void onClick(View v) {

				// TODO Auto-generated method stub

				tvFH.setText(btnCheng.getText());

			    tvResult.setText(Integer.toString(Integer.parseInt(et1.getText().toString())*Integer.parseInt(et2.getText().toString())));

			}

        	

        });

        //除以

        btnChu.setOnClickListener(new Button.OnClickListener(){



			@Override

			public void onClick(View v) {

				// TODO Auto-generated method stub

				tvFH.setText(btnChu.getText());

			    tvResult.setText(Integer.toString(Integer.parseInt(et1.getText().toString())/Integer.parseInt(et2.getText().toString())));

			}

        	

        });

    }

}

运行结果

image

你可能感兴趣的:(Android开发)