安卓端的简单计算器
来源:互联网 发布:c语言区间内素数 编辑:程序博客网 时间:2024/05/01 23:19
1.通过xml来设置按钮样式(创建xml时选择shape)
1)solid设置背景颜色2)<corners android:radius="5dip"/>设置圆角边框3)<gradient android:startColor="@color/colorWhite" android:endColor="@color/colorAccent" />设置渐变色
1.通过xml来设置按钮颜色变化(创建xml时选择sselector)
1)<item android:drawable="@drawable/tu" android:state_pressed="true"/>按下时的颜色2)<item android:drawable="@drawable/or"/>默认状态下(即为按下时的按钮颜色)
在xml里设置计算器上的按钮(这里就不在写了)
在主类中编写代码
package mooc.java.jisuanqi;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity implements OnClickListener{ private Button btn_0; private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Button btn_5; private Button btn_6; private Button btn_7; private Button btn_8; private Button btn_9; private Button btn_jia; private Button btn_jian; private Button btn_cheng; private Button btn_chu; private Button btn_clear; private Button btn_del; private Button btn_equal; private Button btn_point; private EditText et; boolean needclear;//清空表示 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); init(); click(); } protected void click() { btn_0.setOnClickListener(this); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); btn_6.setOnClickListener(this); btn_7.setOnClickListener(this); btn_8.setOnClickListener(this); btn_9.setOnClickListener(this); btn_equal.setOnClickListener(this); btn_jia.setOnClickListener(this); btn_jian.setOnClickListener(this); btn_cheng.setOnClickListener(this); btn_chu.setOnClickListener(this); btn_point.setOnClickListener(this); btn_clear.setOnClickListener(this); btn_del.setOnClickListener(this); } protected void init() { btn_0 = (Button) findViewById(R.id.btn_0); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_6 = (Button) findViewById(R.id.btn_6); btn_7 = (Button) findViewById(R.id.btn_7); btn_8 = (Button) findViewById(R.id.btn_8); btn_9 = (Button) findViewById(R.id.btn_9); btn_jia = (Button) findViewById(R.id.btn_sub); btn_jian = (Button) findViewById(R.id.btn_jian); btn_cheng = (Button) findViewById(R.id.btn_cheng); btn_chu = (Button) findViewById(R.id.btn_chu); btn_equal = (Button) findViewById(R.id.btn_deng); btn_clear = (Button) findViewById(R.id.btn_clear); btn_point = (Button) findViewById(R.id.btn_dian); btn_del = (Button) findViewById(R.id.btn_del); et = (EditText) findViewById(R.id.et); } @Override public void onClick(View v) { String str = et.getText().toString();//得到界面上输入的值,并转成字符串 switch (v.getId()) { case R.id.btn_0: case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: case R.id.btn_4: case R.id.btn_5: case R.id.btn_6: case R.id.btn_7: case R.id.btn_8: case R.id.btn_9: case R.id.btn_dian: if (needclear) { needclear=false; str=""; et.setText(""); } et.setText(str+((Button)v).getText()); break; case R.id.btn_sub: case R.id.btn_jian: case R.id.btn_cheng: case R.id.btn_chu: if (needclear) { needclear=false; } et.setText(str+" "+((Button)v).getText()+" "); break; case R.id.btn_deng: getResult(); break; case R.id.btn_clear: needclear=false; str=""; et.setText(""); break; case R.id.btn_del: if (needclear) { needclear=false; str=""; et.setText(""); }else if(str!=null&&!str.equals("")) { et.setText(str.substring(0,str.length()-1)); } break; default: break; } } //获取运算结果 private void getResult() { String exp = et.getText().toString(); if(exp==null||exp.equals("")) { return; } if(!exp.contains(" ")) { return; } if(needclear) { needclear=false; return; } needclear=true; double r=0; int space = exp.indexOf(" ");//用于搜索空格的位置 String s1 = exp.substring(0, space);//用于保存第一个运算数 String op = exp.substring(space + 1, space + 2);//用于保存运算符 String s2 = exp.substring(space + 3);//s2用于保存第二个运算数 Log.i("exp",exp); Log.i("space",space+""); Log.i("s1",s1); Log.i("op",op); Log.i("s2",s2); if(!s1.equals("")&&!s2.equals("")) { double arg1 = Double.parseDouble(s1);//把运算数由字符串型转换为double型 double arg2 = Double.parseDouble(s2); if(op.equals("+")) { r=arg1+arg2; }else if(op.equals("-")) { r=arg1-arg2; }else if(op.equals("×")) { r=arg1*arg2; }else if(op.equals("÷")) { if (arg2 == 0) { r = 0; } else { r = arg1 / arg2; } }if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {//两个数都是整数 int result = (int)r; et.setText(result+""); }else{ et.setText(r+""); } } else if(!s1.equals("")&&s2.equals("")) { et.setText(exp); }else if(s1.equals("")&&!s2.equals("")) { exp="0 "+exp; space = exp.indexOf(" ");//用于搜索空格的位置 s1 = exp.substring(0, space);//用于保存第一个运算数 op = exp.substring(space + 1, space + 2);//用于保存运算符 s2 = exp.substring(space + 3);//s2用于保存第二个运算数 Log.i("tag",exp); Log.i("tag",space+""); Log.i("tag",s1); Log.i("tag",op); Log.i("tag",s2); double arg2 = Double.parseDouble(s2); if(op.equals("+")) { r=0+arg2; }else if(op.equals("-")) { r=0-arg2; }else if(op.equals("×")) { r=0; }else if(op.equals("÷")) { r=0; }if(!s2.contains(".")) {//两个数都是整数 int result = (int)r; et.setText(result+""); }else{ et.setText(r+""); Log.i("tag",r+""); } }else{ et.setText(""); } }}
0 0
- 安卓端的简单计算器
- 简单的四则计算器
- 简单的计算器程序
- 简单的java计算器
- 简单的计算器
- 小型简单的计算器
- 一个简单的计算器
- 简单的计算器代码
- 简单的计算器制作
- 简单的计算器
- 简单的计算器
- Java 简单的计算器
- ACM简单的计算器
- 一个简单的计算器
- 简单的计算器实现
- 简单的小计算器
- 简单的整数计算器
- 简单的计算器
- Service的使用(一)
- 服务器端用Servlet响应客户端请求,Gson请求。可以返回多个数据库中的信息
- MSP430 定时器输出PWM波形
- C++语言中位运算符在计算中的实际运用
- 2016 Multi-University Training Contest 1 1006 PowMod
- 安卓端的简单计算器
- 7.24——8.23暑期计划
- 【Poj】-1363-Rails(栈)
- Bzoj2839:集合计数:组合数学+容斥
- Android中为什么使用Log,而不适用System.out()?
- java基础Day01
- jq实现有透明遮罩的轻量级弹窗
- Good Bye 2014
- zookeeper 安装 windows环境