android 计算器(2)
16lz
2021-12-04
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()函数,否则显示不出来。 更多相关文章
- android 菜单事件处理
- Android(安卓)计算器
- Android消息机制
- 处理ArcGIS Android工程和ADT v17中的依赖
- Android绘图机制与处理技巧-更新中
- Android的数据处理:使用annotation实现JSON字符串解析为java对象
- Android下添加新的自定义键值和按键处理流程
- 【Android自动化打包】03. APK的数字签名
- Android中的gen文件为空或者不存在的处理方法