通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局 源码如下(欢迎大家指导 批评 )
[java]
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 {
<span style="white-space:pre"> </span>private EditText print;
<span style="white-space:pre"> </span>private static String fistNumber = "0";// 第一次输入的值
<span style="white-space:pre"> </span>private static String secondNumber = "0";// 第二次输入的值
<span style="white-space:pre"> </span>private static String num = "0";// 显示的结果
<span style="white-space:pre"> </span>private static int 易做图 = 0;// 结果累加一次
<span style="white-space:pre"> </span>public Counts take = null;
<span style="white-space:pre"> </span>private int[] btidTake = { R.id.txtdivide, R.id.txtx, R.id.txtmin,
<span style="white-space:pre"> </span>R.id.txttakesum };
<span style="white-space:pre"> </span>private Button[] buttonTake = new Button[btidTake.length];
<span style="white-space:pre"> </span>private int[] btidNum = { R.id.txt0, R.id.txt1, R.id.txt2, R.id.txt3,
<span style="white-space:pre"> </span>R.id.txt4, R.id.txt5, R.id.txt6, R.id.txt7, R.id.txt8, R.id.txt9,
<span style="white-space:pre"> </span>R.id.txtspl };
<span style="white-space:pre"> </span>private Button[] buttons = new Button[btidNum.length];
<span style="white-space:pre"> </span>private int[] btcl = { R.id.chars, R.id.charx, R.id.txtb, R.id.txtv };
<span style="white-space:pre"> </span>private Button[] btcls = new Button[btcl.length];
<span style="white-space:pre"> </span>private GridLayout gly;
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>setContentView(R.layout.activity_main);
<span style="white-space:pre"> </span>gly=(GridLayout)findViewById(R.id.gly);
<span style="white-space:pre"> </span>print = (EditText) findViewById(R.id.print);
<span style="white-space:pre"> </span>print.setText("0");
<span style="white-space:pre"> </span>print.setEnabled(false);
<span style="white-space:pre"> </span>GetNumber get = new GetNumber();
<span style="white-space:pre"> </span>for (int i = 0; i < btidNum.length; i++) {
<span style="white-space:pre"> </span>buttons[i] = (Button) findViewById(btidNum[i]);
<span style="white-space:pre"> </span>buttons[i].setOnClickListener(get);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Compute cm = new Compute();
<span style="white-space:pre"> </span>for (int i = 0; i < btidTake.length; i++) {
<span style="white-space:pre"> </span>buttonTake[i] = (Button) findViewById(btidTake[i]);
<span style="white-space:pre"> </span>buttonTake[i].setOnClickListener(cm);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Button eq = (Button) findViewById(R.id.txteq);
<span style="white-space:pre"> </span>eq.setOnClickListener(new OnClickListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View v) {
<span style="white-space:pre"> </span>if (易做图 == 0) {
<span style="white-space:pre"> </span>secondNumber = print.getText().toString();
<span style="white-space:pre"> </span>if (take == Counts.DIVIDE && secondNumber.equals("0")) {
<span style="white-space:pre"> </span>print.setText("0不能为被除数");
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>num = take.Values(fistNumber, secondNumber);
<span style="white-space:pre"> </span>fi
补充:移动开发 , Android ,