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()函数,否则显示不出来。

更多相关文章

  1. android 菜单事件处理
  2. Android(安卓)计算器
  3. Android消息机制
  4. 处理ArcGIS Android工程和ADT v17中的依赖
  5. Android绘图机制与处理技巧-更新中
  6. Android的数据处理:使用annotation实现JSON字符串解析为java对象
  7. Android下添加新的自定义键值和按键处理流程
  8. 【Android自动化打包】03. APK的数字签名
  9. Android中的gen文件为空或者不存在的处理方法

随机推荐

  1. Android ButterKnife 使用方法总结
  2. 自定义控件:滑动开关按钮
  3. xamarin.forms 使用ZXing扫描二维码
  4. Android懒人框架Android annotation
  5. Android(安卓)Camera API使用指南
  6. 第三方社交网站分享(微信,新浪微博)
  7. Android(安卓)拍照7.0适配(源码)
  8. android 自定义控件基础之三种约束类型
  9. Android(安卓)Jni , 不同 arm 架构 兼容
  10. Activity的正确打开方式