Android学习-计算器3

来源:互联网 发布:sql添加默认值 编辑:程序博客网 时间:2024/05/17 22:07

用了一个星期的时间做了个简单的计算器,算是入门吧。还有很多东西要学。贴上代码……

package application.calculator;import android.R.drawable;import android.app.Activity;import android.os.Bundle;import android.widget.Button;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.TableLayout;import android.widget.TableRow;import android.widget.TextView;import android.graphics.Color;import android.graphics.drawable.ShapeDrawable;import android.graphics.drawable.shapes.RectShape;import android.util.Log;import android.content.ContextWrapper;import android.content.Intent;import android.view.Gravity;import android.view.View.OnClickListener;import android.view.View;public class Calculator extends Activity implements OnClickListener{private static final String TAG = "Debug";private ShapeDrawable rectDrawable;private LinearLayout mainLayout;private TextView textView;private String buttonName[][];private int buttonId[][];private int buttonTextColor[][];private LinearLayout layoutRow[];private final int id_Backspace = 0x0011;private final int id_ce = 0x0012;private final int id_c = 0x0013;private final int id_mc = 0x000b;private final int id_7 = 0x0007;private final int id_8 = 0x0008;private final int id_9 = 0x0009;private final int id_div = 0x0023;private final int id_sqrt = 0x0024;private final int id_mr = 0x000a;private final int id_4 = 0x0004;private final int id_5 = 0x0005;private final int id_6 = 0x0006;private final int id_mul = 0x0022;private final int id_percent = 0x0025;private final int id_ms = 0x0010;private final int id_1 = 0x0001;private final int id_2 = 0x0002;private final int id_3 = 0x0003;private final int id_sub = 0x0021;private final int id_rec = 0x0026;private final int id_mp = 0x0016;private final int id_0 = 0x0000;private final int id_minu = 0x0018;private final int id_point = 0x0019;private final int id_plus = 0x0020;private final int id_equ = 0x001b;private final int id_invalid = 0xffff;private final int opcodePlus = id_plus;private final int opcodeSub = id_sub;private final int opcodeMul = id_mul;private final int opcodeDiv = id_div;private final int opcodeSqrt = id_sqrt;private final int opcodePercent = id_percent;private final int opcodeRec = id_rec;private final int opcodeEqu = id_equ;private final int opcodeInvalid = 0xffff;private float operandA;private float operandB;private float result;private int opcode = opcodeInvalid;private int oldOpcode = opcodeInvalid; private int oldKeyId = id_invalid;private boolean opPressed = false;//private TableLayout tableLayout[];/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState)    {Button button;super.onCreate(savedInstanceState);String buttonName[][] = {{"Backspace","CE","C"},{"MC","7","8","9","/","sqrt"},{"MR","4","5","6","*","%"},{"MS","1","2","3","-","1/x"},{"M+","0","+/-",".","+","="}};int buttonId[][] = {{id_Backspace,id_ce,id_c},{id_mc,id_7,id_8,id_9,id_div,id_sqrt},{id_mr,id_4,id_5,id_6,id_mul,id_div},{id_ms,id_1,id_2,id_3,id_sub,id_rec},{id_mp,id_0,id_minu,id_point,id_plus,id_equ}};int buttonTextColor[][] ={{Color.RED,Color.RED,Color.RED},{Color.RED,Color.BLUE,Color.BLUE,Color.BLUE,Color.RED,Color.BLUE},{Color.RED,Color.BLUE,Color.BLUE,Color.BLUE,Color.RED,Color.BLUE},{Color.RED,Color.BLUE,Color.BLUE,Color.BLUE,Color.RED,Color.BLUE},{Color.RED,Color.BLUE,Color.BLUE,Color.BLUE,Color.RED,Color.RED}};ContextWrapper contextWrapper = new ContextWrapper(this);float scale = contextWrapper.getResources().getDisplayMetrics().density;Log.d(TAG, "scale="+scale);int padPix = (int)(5f*scale + 0.5f);mainLayout = new LinearLayout(this);mainLayout.setOrientation(LinearLayout.VERTICAL);LinearLayout textViewLayout = new LinearLayout(this);textViewLayout.setOrientation(LinearLayout.VERTICAL); textViewLayout.setLayoutParams(new LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,1));textViewLayout.setPadding(padPix, padPix, padPix, padPix);int textSize = (int)(20f*scale+0.5f);textView = new TextView(this);textView.setBackgroundColor(Color.WHITE);textView.setText("0");textView.setTextColor(Color.BLACK);textView.setTextSize(textSize);textView.setLines(5);textView.setGravity(Gravity.RIGHT);textView.setPadding(5, 5, 5, 5);textView.setLayoutParams(new LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,1));textViewLayout.addView(textView);mainLayout.addView(textViewLayout);int length = 0xffff;TableLayout tableLayout = null;for( int i = 0; i < buttonName.length; i++ ){if( buttonName[i].length != length ){tableLayout = new TableLayout(this);tableLayout.setLayoutParams(new LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,0));tableLayout.setStretchAllColumns(true);}TableRow tableRow = new TableRow(this);for( int j = 0; j < buttonName[i].length; j++ ){button = new Button(this);button.setText(buttonName[i][j]);button.setTextColor(buttonTextColor[i][j]);button.setId(buttonId[i][j]);button.setOnClickListener(this);tableRow.addView(button);}tableLayout.addView(tableRow);if( ( i == ( buttonName.length -1 )) || (buttonName[i].length != buttonName[i+1].length) ){mainLayout.addView(tableLayout);}length = buttonName[i].length;}rectDrawable = new ShapeDrawable( new RectShape() );rectDrawable.getPaint().setColor( Color.GRAY );rectDrawable.setPadding(5, 5, 5, 5);mainLayout.setBackgroundDrawable(rectDrawable);setContentView(mainLayout);   }public void onClick(View v){final int buttonId = v.getId();switch(buttonId){case id_0:case id_1:case id_2:case id_3:case id_4:case id_5:case id_6:case id_7:case id_8:case id_9:{appendText(Integer.toString(buttonId));break;}case id_point:{appendText(".");break;}case id_c:case id_ce:{textView.setText("0");operandA = 0;operandB = 0;oldOpcode = opcodeInvalid;break;}case id_plus:case id_sub:case id_mul:case id_div:case id_percent:case id_sqrt:case id_rec:{opPressed = true;opcode = buttonId;procOpcode();break;}case id_equ:{getResult();break;}case id_Backspace:{detOneChar();break;}default:{Intent error = new Intent(this,Error.class);startActivity(error);textView.setText("0");operandA = 0;operandB = 0;oldOpcode = opcodeInvalid;break;}}oldKeyId = buttonId;}private void appendText( CharSequence text ){CharSequence oldText;oldText = textView.getText();if( (Float.valueOf(oldText.toString()) == 0) || (opPressed == true)){opPressed = false;textView.setText(text);}else{textView.append(text);}}private void procOpcode( ){if( oldOpcode != opcodeInvalid ){getResult();}operandA = Float.valueOf(textView.getText().toString());switch(opcode){case opcodePercent:{result = operandA/100f;getResult();break;}case opcodeSqrt:{if( operandA < 0 ){Intent error = new Intent(this,Error.class);startActivity(error);textView.setText("0");operandA = 0;operandB = 0;oldOpcode = opcodeInvalid;}else{result = (float)Math.sqrt(operandA);getResult();}break;}case opcodeRec:{if( operandA == 0 ){Intent error = new Intent(this,Error.class);startActivity(error);textView.setText("0");operandA = 0;operandB = 0;oldOpcode = opcodeInvalid;}else{result = 1f/operandA;getResult();}break;}default:{break;}}oldOpcode = opcode;}private void getResult(){float tempOperand;tempOperand = Float.valueOf(textView.getText().toString());Log.d(TAG, "tempOperand ="+tempOperand);if( oldKeyId != id_equ ){Log.d(TAG, "oldKeyId != id_equ");operandB = tempOperand;}else{Log.d(TAG, "oldKeyId == id_equ");operandA = tempOperand;}switch(opcode){case opcodePlus:{Log.d(TAG, "operandA ="+operandA);Log.d(TAG, "operandB="+operandB);result = operandA + operandB;break;}case opcodeSub:{result = operandA - operandB;break;}case opcodeMul:{result = operandA * operandB;break;}case opcodeDiv:{if( operandB == 0 ){Intent error = new Intent(this,Error.class);startActivity(error);textView.setText("0");operandA = 0;operandB = 0;oldOpcode = opcodeInvalid;result = 0;}else{result = operandA / operandB;}break;}default:{break;}}if( (result - (int)result) == 0 ){textView.setText(String.valueOf((int)result));}else{textView.setText(String.valueOf(result));}oldOpcode = opcodeInvalid;}private void detOneChar(){String text = textView.getText().toString();if( text.length() == 1 ){text = "0";}else{text = text.substring(0,text.length()-1);}textView.setText(text);}}

下一步打算做一个MP3播放器

原创粉丝点击