安卓版計算器,隻實現簡單功能,Android 2.2真機測試,
佈局采用TableLayout,main.xml代碼如下:
[html] <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:textSize="32sp" >
<TableRow>
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:cursorVisible="false"
android:editable="false"
android:gravity="right|center_vertical"
android:lines="1"
android:textSize="32sp" />
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="32sp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="32sp" />
<Button
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C"
android:layout_weight="1"
android:textSize="32sp" />
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="32sp" />
<Button
android:id="@+id/num8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="32sp" />
<Button
android:id="@+id/num9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="32sp" />
<Button
android:id="@+id/subtract"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="32sp" />
<Button
android:id="@+id/num5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="32sp" />
<Button
android:id="@+id/num6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="32sp" />
<Button
android:id="@+id/multiply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="32sp" />
<Button
android:id="@+id/num2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="32sp" />
<Button
android:id="@+id/num3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="32sp" />
<Button
android:id="@+id/pide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textSize="32sp" />
<Button
android:id="@+id/point"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textSize="32sp" />
<Button
android:id="@+id/negative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="32sp" />
<Button
android:id="@+id/equal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="32sp" />
</LinearLayout>
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:textSize="32sp" >
<TableRow>
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:cursorVisible="false"
android:editable="false"
android:gravity="right|center_vertical"
android:lines="1"
android:textSize="32sp" />
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="32sp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="32sp" />
<Button
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C"
android:layout_weight="1"
android:textSize="32sp" />
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="32sp" />
<Button
android:id="@+id/num8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="32sp" />
<Button
android:id="@+id/num9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="32sp" />
<Button
android:id="@+id/subtract"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="32sp" />
<Button
android:id="@+id/num5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="32sp" />
<Button
android:id="@+id/num6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="32sp" />
<Button
android:id="@+id/multiply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="32sp" />
<Button
android:id="@+id/num2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="32sp" />
<Button
android:id="@+id/num3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="32sp" />
<Button
android:id="@+id/pide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="32sp" />
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
<Button
android:id="@+id/num0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textSize="32sp" />
<Button
android:id="@+id/point"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textSize="32sp" />
<Button
android:id="@+id/negative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="32sp" />
<Button
android:id="@+id/equal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="32sp" />
</LinearLayout>
</TableRow>
</TableLayout>
下面是CalculatorActivity.java代碼,有詳細註釋:
[java] public class CalculatorActivity extends Activity implements OnClickListener{
EditText editText;
Button num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;
Button clear,add,subtract,multiply,pide,point,negative,equal;
double firNum=0,secNum=0;
//運算符號
char c;
//小數點控制
boolean dot=true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
num0=(Button)findViewById(R.id.num0);
num1=(Button)findViewById(R.id.num1);
num2=(Button)findViewById(R.id.num2);
num3=(Button)findViewById(R.id.num3);
num4=(Button)findViewById(R.id.num4);
num5=(Button)findViewById(R.id.num5);
num6=(Button)findViewById(R.id.num6);
num7=(Button)findViewById(R.id.num7);
num8=(Button)findViewById(R.id.num8);
num9=(Button)findViewById(R.id.num9);
clear=(Button)findViewById(R.id.clear);
editText=(EditText)findViewById(R.id.editText);
add =(Button) findViewById(R.id.add);
subtract =(Button) findViewById(R.id.subtract);
multiply =(Button) findViewById(R.id.multiply);
pide =(Button) findViewById(R.id.pide);
point=(Button)findViewById(R.id.point);
negative =(Button) findViewById(R.id.negative);
equal =(Button) findViewById(R.id.equal);
//註冊單擊事件
num0.setOnClickListener(this);
num1.setOnClickListener(this);
num2.setOnClickListener(this);
num3.setOnClickListener(this);
num4.setOnClickListener(this);
num5.setOnClickListener(this);
num6.setOnClickListener(this);
num7.setOnClickListener(this);
num8.setOnClickListener(this);
num9.setOnClickListener(this);
add.setOnClickListener(this);
clear.setOnClickListener(this);
subtract.setOnClickListener(this);
multiply.setOnClickListener(this);
pide.setOnClickListener(this);
point.setOnClickListener(this);
negative.setOnClickListener(this);
equal.setOnClickListener(this);
}
//響應單擊事件
public void onClick(View v) {
if(v.equals(num0)){
editText.setText(editText.getText()+"0");
}
if(v.equals(num1)){
editText.setText(editText.getText()+"1");
}
if(v.equals(num2)){
editText.setText(editText.getText()+"2");
}
if(v.equals(num3)){
editText.setText(editText.getText()+"3");
}
if(v.equals(num4)){
editText.setText(editText.getText()+"4");
}
if(v.equals(num5)){
editText.setText(editText.getText()+"5");
}
if(v.equals(num6)){
editText.setText(editText.getText()+"6");
}
if(v.equals(num7)){
editText.setText(editText.getText()+"7");
}
if(v.equals(num8)){
editText.setText(editText.getText()+"8");
}
if(v.equals(num9)){
editText.setText(editText.getText()+"9");
}
if(v.equals(clear)){
editText.setText("");
}
//如果editText為空
if(editText.getText().toString().equalsIgnoreCase("")){
Toast.makeText(this, "沒有輸入數字!", Toast.LENGTH_SHORT).show();
}else {
if(v.equals(add)){
c='+';
firNum=Double.parseDouble(editText.getText().toString());
editText.setText("");
}
if(v.equals(subtract)){
c='-';
firNum=Double.parseDouble(editText.getText().toString());
editText.setText("");
}
if(v.equals(multiply)){
c='*';
firNum=Double.parseDouble(editText.getText().toString());
editText.setText("");
}
if(v.equals(pide)){
c='/';
firNum=Double.parseDouble(editText.getText().toString());
editText.setText("");
}
if(v.equals(point)){
dot=false;
editText.setText(editText.getText()+".");
}
//等於
if(v.equals(equal)){
double result=0;
secNum=Double.parseDouble(editText.getText().toString());
if(c=='+'){
result=firNum+secNum;
}
if(c=='-'){
result=firNum-secNum;
}
if(c=='*'){
result=firNum*secNum;
}
if(c=='/'){
if(secNum==0){
Toast.makeText(this, "被除數不能為零!", Toast.LENGTH_SHORT).show();
editText.setText("");
}else{
result=firNum/secNum;
}
}
editText.setText(result+"");
}
}
if(v.equals(negative)){
editText.setText(editText.getText()+"-");
}
}
//重寫onCreateOptionsMenu()方法,創建菜單,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//創建菜單,並設置圖標,使用系統提供的圖標,
menu.add(Menu.NONE, Menu.FIRST + 1, 1, "關於").setIcon(android.R.drawable.ic_dialog_info);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "XX").setIcon(android.R.drawable.ic_lock_idle_alarm);
menu.add(Menu.NONE, Menu.FIRST + 3, 2,"退出").setIcon(android.R.drawable.ic_lock_power_off);;
// 返回true即顯示菜單
return true;
}
@Override
//響應菜單單擊事件,
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==Menu.FIRST + 1){
//關於
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("關於");
dialog.setMessage("簡易計算器!\nBy:柒少狐\nQQ:569629066");
dialog.show();
}
if(item.getItemId()==Menu.FIRST + 1){
Toast.makeText(this, "我那個去,", Toast.LENGTH_SHORT).show();
}
if(item.getItemId()==Menu.FIRST + 3){
//退出
this.finish();
}
return true;
}
}
摘自 柒尐狐的專欄