实现一个只能加的计算器
xlm:
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.xialm.calculator.MainActivity"> <EditText android:id="@+id/ed_im1" android:layout_width="match_parent" android:layout_height="70dp" android:hint="输入栏" /> <EditText android:id="@+id/ed_im2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="结果" android:layout_below="@+id/ed_im1" /> <GridLayout android:id="@+id/number" android:layout_width="match_parent" android:layout_height="wrap_content" android:rowCount="5" android:orientation="horizontal" android:columnCount="4" android:layout_below="@+id/ed_im2" android:layout_marginTop="5dp" > <Button android:id="@+id/bu_1" android:layout_width="wrap_content" android:layout_height="80dp" android:text="1" android:layout_row="0" android:layout_column="0" /> <Button android:id="@+id/bu_2" android:layout_width="wrap_content" android:layout_height="80dp" android:text="2" android:layout_row="0" android:layout_column="1" /> <Button android:id="@+id/bu_3" android:layout_width="wrap_content" android:layout_height="80dp" android:text="3" android:layout_row="0" android:layout_column="2" /> <Button android:id="@+id/bu_num" android:layout_width="wrap_content" android:layout_height="80dp" android:text="+" android:layout_gravity="fill" android:layout_rowSpan="2" android:layout_row="0" android:layout_column="3" android:textSize="50px" android:gravity="center" /> <Button android:id="@+id/bu_4" android:layout_width="wrap_content" android:layout_height="80dp" android:text="4" android:layout_row="1" android:layout_column="0" /> <Button android:id="@+id/bu_5" android:layout_width="wrap_content" android:layout_height="80dp" android:text="5" android:layout_row="1" android:layout_column="1" /> <Button android:id="@+id/bu_6" android:layout_width="wrap_content" android:layout_height="80dp" android:text="6" android:layout_row="1" android:layout_column="2" /> <Button android:id="@+id/bu_7" android:layout_width="wrap_content" android:layout_height="80dp" android:text="7" android:layout_row="2" android:layout_column="0" /> <Button android:id="@+id/bu_8" android:layout_width="wrap_content" android:layout_height="80dp" android:text="8" android:layout_row="2" android:layout_column="1" /> <Button android:id="@+id/bu_9" android:layout_width="wrap_content" android:layout_height="80dp" android:text="9" android:layout_row="2" android:layout_column="2" /> <Button android:id="@+id/bu_eq" android:layout_width="wrap_content" android:layout_height="80dp" android:text="=" android:layout_rowSpan="2" android:layout_gravity="fill" android:layout_row="2" android:layout_column="3" /> <Button android:id="@+id/bu_0" android:layout_width="wrap_content" android:layout_height="80dp" android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill" android:layout_row="3" android:layout_column="0" /> <Button android:id="@+id/bu_point" android:layout_width="wrap_content" android:layout_height="80dp" android:text="." android:layout_row="3" android:layout_column="2" /> <Button android:id="@+id/bu_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除" android:layout_row="4" android:layout_column="2" android:layout_columnSpan="2" android:layout_gravity="fill" /> </GridLayout> </RelativeLayout>
mainactivity
package com.example.xialm.calculator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bnum,beq,bpoint,bclear; private EditText et_1,et_2; static String data="",data_in = ""; static int result=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到我们关心的控件 b1 = (Button)findViewById(R.id.bu_1); b2 = (Button)findViewById(R.id.bu_2); b3 = (Button)findViewById(R.id.bu_3); b4 = (Button)findViewById(R.id.bu_4); b5 = (Button)findViewById(R.id.bu_5); b6 = (Button)findViewById(R.id.bu_6); b7 = (Button)findViewById(R.id.bu_7); b8 = (Button)findViewById(R.id.bu_8); b9 = (Button)findViewById(R.id.bu_9); b0 = (Button)findViewById(R.id.bu_0); bnum =(Button)findViewById(R.id.bu_num); beq = (Button)findViewById(R.id.bu_eq); bpoint = (Button)findViewById(R.id.bu_point); bclear =(Button)findViewById(R.id.bu_clear); b1.setOnClickListener(MainActivity.this); b2.setOnClickListener(MainActivity.this); b3.setOnClickListener(MainActivity.this); b4.setOnClickListener(MainActivity.this); b5.setOnClickListener(MainActivity.this); b6.setOnClickListener(MainActivity.this); b7.setOnClickListener(MainActivity.this); b8.setOnClickListener(MainActivity.this); b9.setOnClickListener(MainActivity.this); b0.setOnClickListener(MainActivity.this); bnum.setOnClickListener(MainActivity.this); beq.setOnClickListener(MainActivity.this); bpoint.setOnClickListener(MainActivity.this); bclear.setOnClickListener(MainActivity.this); et_2 = (EditText)findViewById(R.id.ed_im1); et_1 = (EditText)findViewById(R.id.ed_im2); } public void onClick(View v){ //具体判断点击的是哪个按钮 switch(v.getId()){ case R.id.bu_0://代表点击第一个按钮 data+="0"; data_in+="0"; //按一个0,就加一个0 et_2.setText(data); //实现游标确定位置 et_2.setSelection(data.length()); break; case R.id.bu_1: data+="1"; data_in+="1"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_2:// data+="2"; data_in+="2"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_3: data+="3"; data_in+="3"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_4: data+="4"; data_in+="4"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_5: data+="5"; data_in+="5"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_6: data+="6"; data_in+="6"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_7: data+="7"; data_in+="7"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_8: data+="8"; data_in+="8"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_9: data+="9"; data_in+="9"; et_2.setText(data); et_2.setSelection(data.length()); break; case R.id.bu_point: data+="."; data_in+="."; et_2.setText(data); et_2.setSelection(data.length()); break; //运算加的逻辑,在加上一个数之后,再加一个a //所以加一个数字的时候,15+1,在data_in中是15a1 //这里因为想使用+来分割字符发现这样做不合法 //于是无奈搞出两个变量data和data_in //实际上计算的是data_in,显示的是data case R.id.bu_num: data+="+"; data_in+="a"; et_2.setText(data); et_2.setSelection(data.length()); break; //等于的逻辑,先将data_in这个数据使用split这样由a分成一个数组 //再遍历相加。 case R.id.bu_eq: String[] strs = data_in.split("a"); //String[] strs = str.split(":"); int result1=0; for (int i=0;i<strs.length;i++) { result1+=Integer.parseInt(strs[i]); } String okok = result1+""; et_1.setText(okok); break; case R.id.bu_clear: data=""; data_in=""; result1=0; et_2.setText(data); et_2.setSelection(data.length()); et_1.setText(data); et_2.clearFocus();//不显示光标 et_2.setSelection(data.length()); break; default: break; } } }