Created
January 18, 2011 14:31
-
-
Save kor-trickster/784497 to your computer and use it in GitHub Desktop.
android - 계산기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////// TestActivity ////////////////////////////////////// | |
package com.begin; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
public class TestActivity extends Activity { | |
/** Called when the activity is first created. */ | |
private int num = 0; | |
private int tot = 0; | |
private char flag; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
((Button) findViewById(R.id.Button01)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button02)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button03)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button04)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button05)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button06)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button07)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button08)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button09)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.Button00)).setOnClickListener(on_ClickNum); | |
((Button) findViewById(R.id.plus)).setOnClickListener(on_ClickOper); | |
((Button) findViewById(R.id.minus)).setOnClickListener(on_ClickOper); | |
((Button) findViewById(R.id.mul)).setOnClickListener(on_ClickOper); | |
((Button) findViewById(R.id.div)).setOnClickListener(on_ClickOper); | |
((Button) findViewById(R.id.result)).setOnClickListener(new Button.OnClickListener(){ | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
String result = null; | |
EditText et = ((EditText) findViewById(R.id.EditText01)); | |
switch(flag){ | |
case '+' : | |
tot += num; | |
break; | |
case '*' : | |
tot *= num; | |
break; | |
case '-' : | |
tot -= num; | |
break; | |
case '/' : | |
tot /= num; | |
break; | |
default : | |
break; | |
} | |
result = Integer.toString(tot); | |
et.setText(result); | |
num = tot; | |
} | |
}); | |
((Button) findViewById(R.id.clear)).setOnClickListener(new Button.OnClickListener(){ | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
EditText et = ((EditText) findViewById(R.id.EditText01)); | |
num = 0; | |
et.setText("0"); | |
} | |
}); | |
} | |
private View.OnClickListener on_ClickOper = new View.OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
tot = num; | |
num = 0; | |
switch(v.getId()){ | |
case R.id.plus : | |
flag = '+'; | |
break; | |
case R.id.minus : | |
flag = '-'; | |
break; | |
case R.id.mul: | |
flag = '*'; | |
break; | |
case R.id.div : | |
flag = '/'; | |
break; | |
default : | |
break; | |
} | |
} | |
}; | |
private View.OnClickListener on_ClickNum = new View.OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
String id = null; | |
String s; | |
EditText et = ((EditText) findViewById(R.id.EditText01)); | |
switch(v.getId()){ | |
case R.id.Button00 : | |
id = "0"; | |
break; | |
case R.id.Button01 : | |
id = "1"; | |
break; | |
case R.id.Button02 : | |
id = "2"; | |
break; | |
case R.id.Button03 : | |
id = "3"; | |
break; | |
case R.id.Button04 : | |
id = "4"; | |
break; | |
case R.id.Button05 : | |
id = "5"; | |
break; | |
case R.id.Button06 : | |
id = "6"; | |
break; | |
case R.id.Button07 : | |
id = "7"; | |
break; | |
case R.id.Button08 : | |
id = "8"; | |
break; | |
case R.id.Button09 : | |
id = "9"; | |
break; | |
default : | |
break; | |
} | |
if(num == 0) et.setText(""); | |
s = et.getText() + id; | |
et.setText(s); | |
num = Integer.parseInt(s); | |
} | |
}; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////// main.xml ////////////////////////////////////// | |
<?xml version="1.0" ENCODING="utf-8"?> | |
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<EditText android:text="0" android:id="@+id/EditText01" | |
android:layout_width="wrap_content" android:layout_height="wrap_content" | |
></EditText> | |
<TableRow> | |
<Button android:text="1" android:id="@+id/Button01" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="2" android:id="@+id/Button02" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="3" android:id="@+id/Button03" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="+" android:id="@+id/plus" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
<TableRow> | |
<Button android:text="4" android:id="@+id/Button04" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="5" android:id="@+id/Button05" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="6" android:id="@+id/Button06" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="-" android:id="@+id/minus" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
<TableRow> | |
<Button android:text="7" android:id="@+id/Button07" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="8" android:id="@+id/Button08" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="9" android:id="@+id/Button09" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="*" android:id="@+id/mul" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
<TableRow> | |
<Button android:text="0" android:id="@+id/Button00" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="=" android:id="@+id/result" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="clear" android:id="@+id/clear" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="/" android:id="@+id/div" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
</TableLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment