android 计算器(2)

package com.example.calculater;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
	public final static String EXTRA_MESSAGE = "calulater.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main); 
        
    }
    
    public void Add(View view){
    	try{
    	 EditText editText1 = (EditText) findViewById(R.id.num1);
      	 EditText editText2 = (EditText) findViewById(R.id.num2);
    	    String message1 = editText1.getText().toString();
    	    String message2 = editText2.getText().toString();
    	 float result;
    	 result=Float.parseFloat(message1)+Float.parseFloat(message2); 
    	 TextView textview=(TextView)findViewById(R.id.result1);
    	 textview.setText(String.valueOf(result));
    }
    	catch(NumberFormatException e1){Toast.makeText(this, "only input integer please", 3).show();}}
    public void Sub(View view){
    	try{ 
    	EditText editText1 = (EditText) findViewById(R.id.num1);
      	 EditText editText2 = (EditText) findViewById(R.id.num2);
    	    String message1 = editText1.getText().toString();
    	    String message2 = editText2.getText().toString();
    	 float result;
    	 result=Float.parseFloat(message1)-Float.parseFloat(message2);
    	 TextView textview=(TextView)findViewById(R.id.result1);
    	 textview.setText(String.valueOf(result));
    }
    	catch(NumberFormatException e2){Toast.makeText(this, "only input integer please", 3).show();}
    }
    
    public void Mul(View view){
    	try{ 
    	EditText editText1 = (EditText) findViewById(R.id.num1);
      	 EditText editText2 = (EditText) findViewById(R.id.num2);
    	    String message1 = editText1.getText().toString();
    	    String message2 = editText2.getText().toString();
    	 float result;
    	 result=Float.parseFloat(message1)*Float.parseFloat(message2);
    	 TextView textview=(TextView)findViewById(R.id.result1);
    	 textview.setText(String.valueOf(result));
    }
    	catch(NumberFormatException e2){Toast.makeText(this, "only input integer please", 3).show();}
    }

    public void Div(View view){
    	try{ 
    	EditText editText1 = (EditText) findViewById(R.id.num1);
      	 EditText editText2 = (EditText) findViewById(R.id.num2);
    	    String message1 = editText1.getText().toString();
    	    String message2 = editText2.getText().toString();
    	 float result;
    	 result=Float.parseFloat(message1)/Float.parseFloat(message2);
    	 TextView textview=(TextView)findViewById(R.id.result1);
    	 textview.setText(String.valueOf(result));
    }
    	catch(NumberFormatException e2){Toast.makeText(this, "only input integer please", 3).show();}
    }
  /*   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }*/

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

源码如上,主要是添加button的单击事件的处理方法add,sub,mul,div.用了try-catch 结构处理异常。
ps:numberFormatException是java提供的数字格式化异常。做了toast,发现 输入的数据不是数字就会toast个提示。toast最后结尾要有个show()函数,否则显示不出来。

猜你喜欢

转载自blog.csdn.net/dok12/article/details/80782623