通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局 源码如下(欢迎大家指导 批评 )
https://github.com/xionglihui源码下载地址:http://download.csdn.net/detail/x605940745/7148153
转载请注明出处:http://blog.csdn.net/x605940745
package com.android.xiong.gridlayoutTest; import java.math.BigDecimal; import java.util.regex.Pattern; import com.android.xiong.gridlayoutTest.R.id; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.GridLayout; import android.widget.TextView; public class MainActivity extends Activity { private EditText print; private static String fistNumber = "0";// 第一次输入的值 private static String secondNumber = "0";// 第二次输入的值 private static String num = "0";// 显示的结果 private static int flg = 0;// 结果累加一次 public Counts take = null; private int[] btidTake = { R.id.txtdivide, R.id.txtx, R.id.txtmin, R.id.txttakesum }; private Button[] buttonTake = new Button[btidTake.length]; private int[] btidNum = { R.id.txt0, R.id.txt1, R.id.txt2, R.id.txt3, R.id.txt4, R.id.txt5, R.id.txt6, R.id.txt7, R.id.txt8, R.id.txt9, R.id.txtspl }; private Button[] buttons = new Button[btidNum.length]; private int[] btcl = { R.id.chars, R.id.charx, R.id.txtb, R.id.txtv }; private Button[] btcls = new Button[btcl.length]; private GridLayout gly; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gly=(GridLayout)findViewById(R.id.gly); print = (EditText) findViewById(R.id.print); print.setText("0"); print.setEnabled(false); GetNumber get = new GetNumber(); for (int i = 0; i < btidNum.length; i++) { buttons[i] = (Button) findViewById(btidNum[i]); buttons[i].setOnClickListener(get); } Compute cm = new Compute(); for (int i = 0; i < btidTake.length; i++) { buttonTake[i] = (Button) findViewById(btidTake[i]); buttonTake[i].setOnClickListener(cm); } Button eq = (Button) findViewById(R.id.txteq); eq.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (flg == 0) { secondNumber = print.getText().toString(); if (take == Counts.DIVIDE && secondNumber.equals("0")) { print.setText("0不能为被除数"); } else { num = take.Values(fistNumber, secondNumber); fistNumber = num; secondNumber = "0"; print.setText(num); flg = 1; gly.setBackgroundResource(R.drawable.jz); } } } }); Button cleargo = (Button) findViewById(R.id.cleargo); cleargo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (num.length() > 1) { num = num.substring(0, num.length() - 1); } else { num = "0"; } print.setText(num); } }); Button clear = (Button) findViewById(R.id.clear); clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub num = "0"; fistNumber = secondNumber = num; print.setText(num); flg = 0; } }); for (int i = 0; i < btcl.length; i++) { btcls[i] = (Button) findViewById(btcl[i]); btcls[i].setOnClickListener(new OnTake()); } } // 给 EditText赋值 class GetNumber implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub if (flg == 1) num = "0"; if (num.equals("0")) { print.setText(""); num = v.getId() == R.id.txtspl ? "0" : ""; } String txt = ((Button) v).getText().toString(); boolean s = Pattern.matches("-*(\\d+).?(\\d)*", num + txt); num = s ? (num + txt) : num; gly.setBackgroundResource(R.drawable.js); print.setText(num); } } // 根据条件计算 class Compute implements OnClickListener { @Override public void onClick(View arg0) { fistNumber = print.getText().toString(); // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.txttakesum: take = Counts.ADD; break; case R.id.txtmin: take = Counts.MINUS; break; case R.id.txtx: take = Counts.MULTIPLY; break; case R.id.txtdivide: take = Counts.DIVIDE; break; } num = "0"; flg = 0; gly.setBackgroundResource(R.drawable.js); } } class OnTake implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.chars: num = "-" + num; break; case R.id.charx: if(!num.equals("0")) num = BigDecimal.valueOf(1).divide(new BigDecimal(num),12,BigDecimal.ROUND_UP).stripTrailingZeros() .toString(); break; case R.id.txtb: num = new BigDecimal(num).divide(BigDecimal.valueOf(100),12,BigDecimal.ROUND_UP).stripTrailingZeros() .toString(); break; case R.id.txtv: Double numss = Math.sqrt(new BigDecimal(num).doubleValue()); int stratindex=numss.toString().contains(".")?numss.toString().indexOf("."):0; num = numss.toString().length()>13?numss.toString().substring(0, 12+stratindex):numss.toString(); } print.setText(num); flg=0; num = "0"; } } @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; } }
<pre code_snippet_id="275884" snippet_file_name="blog_20140404_2_5383103">
package com.android.xiong.gridlayoutTest; import java.math.BigDecimal; public enum Counts { ADD, MINUS, MULTIPLY, DIVIDE, MARK, ROOT; public String Values(String num1, String num2) { BigDecimal number1 = new BigDecimal(num1); BigDecimal number2 = new BigDecimal(num2); BigDecimal number = BigDecimal.valueOf(0); switch (this) { case ADD: number = number1.add(number2); break; case MINUS: number = number1.subtract(number2); break; case MULTIPLY: number = number1.multiply(number2); break; case DIVIDE: number = number1.divide(number2,20,BigDecimal.ROUND_UP); break; } return number.stripTrailingZeros().toString(); } }
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40px" android:columnCount="5" android:rowCount="6" tools:context=".MainActivity" > <EditText android:id="@+id/print" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="5" android:layout_marginLeft="2px" android:layout_marginRight="2px" android:layout_row="0" android:background="#eee" /> <Button android:id="@+id/cleargo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="1" android:text="《--" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3" android:layout_columnSpan="2" android:layout_row="1" android:layout_gravity="fill_horizontal" android:text="清屏" /> <Button android:id="@+id/chars" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="1" android:text="-/+" /> <Button android:id="@+id/charx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" android:layout_row="1" android:text="1/x" /> <Button android:id="@+id/txt7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="2" android:text="7" /> <Button android:id="@+id/txt8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="2" android:text="8" /> <Button android:id="@+id/txt9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" android:layout_row="2" android:text="9" /> <Button android:id="@+id/txtdivide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3" android:layout_row="2" android:text="÷" /> <Button android:id="@+id/txtb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4" android:layout_row="2" android:text="%" /> <Button android:id="@+id/txt4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="3" android:text="4" /> <Button android:id="@+id/txt5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="3" android:text="5" /> <Button android:id="@+id/txt6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" android:layout_row="3" android:text="6" /> <Button android:id="@+id/txtx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3" android:layout_row="3" android:text="X" /> <Button android:id="@+id/txtv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4" android:layout_row="3" android:text="√" /> <Button android:id="@+id/txt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="4" android:text="1" /> <Button android:id="@+id/txt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="4" android:text="2" /> <Button android:id="@+id/txt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" android:layout_row="4" android:text="3" /> <Button android:id="@+id/txtmin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3" android:layout_row="4" android:text="-" /> <Button android:id="@+id/txteq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4" android:layout_gravity="fill_vertical" android:layout_row="4" android:layout_rowSpan="2" android:text="=" /> <Button android:id="@+id/txt0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:layout_row="5" android:text="0" /> <Button android:id="@+id/txtspl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" android:layout_row="5" android:text="." /> <Button android:id="@+id/txttakesum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3" android:layout_row="5" android:text="+" /> </GridLayout>