亲宝软件园·资讯

展开

android计算器 android实现简易计算器

small菜鸟 人气:0

效果图:

一、如图,首先布局计算器主页显示

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
 
 <EditText
  android:id="@+id/num1"
  android:layout_width="80dp"
  android:layout_height="wrap_content" 
  android:inputType="number" />
 
 <TextView 
  android:id="@+id/type"
  android:layout_width="40dp"
  android:layout_height="50dp"
  android:layout_toRightOf="@id/num1"
  android:gravity="center" />
 
 <EditText
  android:id="@+id/num2"
  android:layout_width="80dp"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/num1"
  android:layout_marginLeft="50dp"
  android:inputType="number" />
 
 <TextView
  android:id="@+id/equal"
  android:layout_width="80dp"
  android:layout_height="50dp"
  android:layout_toRightOf="@id/num2"
  android:gravity="center" />
 
 <Button 
  android:id="@+id/add"
  android:layout_width="70dp"
  android:layout_height="wrap_content"
  android:layout_below="@id/num1"
  android:text="@string/add" />
 
 <Button 
  android:id="@+id/sub"
  android:layout_width="70dp"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/add"
  android:layout_alignTop="@id/add"
  android:text="@string/sub" />
 
 <Button 
  android:id="@+id/mul"
  android:layout_width="70dp"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/sub"
  android:layout_alignTop="@id/sub"
  android:text="@string/mul" />
 
 <Button 
  android:id="@+id/div"
  android:layout_width="70dp"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/mul"
  android:layout_alignTop="@id/mul"
  android:text="@string/div" />
</RelativeLayout>

二、计算器实现的方法

public class CalculatorBiz {
 
 public float add(float num1,float num2){
 return num1+num2;
 }
 
 public float sub(float num1,float num2){
 return num1-num2;
 }
 
 public float mul(float num1,float num2){
 return num1*num2;
 }
 
 public float div(float num1,float num2){
 return num1/num2;
 }
 
}

三、MainActivity主要方法

public class MainActivity extends Activity {
 //定义组件
 private EditText inputnum1;
 private EditText inputnum2;
 private TextView type;
 private TextView equal;
 private Button add;
 private Button sub;
 private Button mul;
 private Button div;
 CalculatorBiz calculator=new CalculatorBiz();
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //查找组件
  inputnum1=(EditText) findViewById(R.id.num1);
  inputnum2=(EditText) findViewById(R.id.num2);
  type=(TextView) findViewById(R.id.type);
  equal=(TextView) findViewById(R.id.equal);
  add=(Button) findViewById(R.id.add);
  sub=(Button) findViewById(R.id.sub);
  mul=(Button) findViewById(R.id.mul);
  div=(Button) findViewById(R.id.div);
  
  type.setTextColor(Color.WHITE);
  equal.setTextColor(Color.WHITE);
  
  add.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 String num1=inputnum1.getText().toString();
 String num2=inputnum2.getText().toString();
 float a=Float.parseFloat(num1);
 float b=Float.parseFloat(num2);
 type.setText("+");
 equal.setText("="+calculator.add(a, b));
 
 
 }});
  sub.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 String num1=inputnum1.getText().toString();
 String num2=inputnum2.getText().toString();
 float a=Float.parseFloat(num1);
 float b=Float.parseFloat(num2);
 type.setText("-");
 equal.setText("="+calculator.sub(a, b));
 
 
 }});
  mul.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 String num1=inputnum1.getText().toString();
 String num2=inputnum2.getText().toString();
 float a=Float.parseFloat(num1);
 float b=Float.parseFloat(num2);
 type.setText("*");
 equal.setText("="+calculator.mul(a, b));
 
 
 }});
  div.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
 String num1=inputnum1.getText().toString();
 String num2=inputnum2.getText().toString();
 float a=Float.parseFloat(num1);
 float b=Float.parseFloat(num2);
 type.setText("/");
 equal.setText("="+calculator.div(a, b));
 
 
 }});
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, 1, 0, "关于");
  menu.add(0, 2, 1, "退出");
  menu.findItem(2);
  return true;
 }
 public boolean onOptionsItemSelected(MenuItem item){
  if(item.getItemId() == 1){   
  Toast.makeText(this, "详细内容\n"+"作者:dyp\n"+"版本:version1", Toast.LENGTH_LONG).show();  
  }else if(item.getItemId() == 2){   
  Toast.makeText(this, "退出\n"+"你确定退出程序?", Toast.LENGTH_LONG).show();  
  }  
  return super.onOptionsItemSelected(item);
 }
 
 
}

加载全部内容

相关教程
猜你喜欢
用户评论