1.MainActivity.java
package wei.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
TextView tv1;
Boolean bBegin=true;
double Data=0;
String Operator="=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviews();
}
void findviews(){
tv1=(TextView)findViewById(R.id.textView1);
b0=(Button)findViewById(R.id.bt0);
b0.setOnClickListener(btlistener);
b1=(Button)findViewById(R.id.bt1);
b1.setOnClickListener(btlistener);
b2=(Button)findViewById(R.id.bt2);
b2.setOnClickListener(btlistener);
b3=(Button)findViewById(R.id.bt3);
b3.setOnClickListener(btlistener);
b4=(Button)findViewById(R.id.bt4);
b4.setOnClickListener(btlistener);
b5=(Button)findViewById(R.id.bt5);
b5.setOnClickListener(btlistener);
b6=(Button)findViewById(R.id.bt6);
b6.setOnClickListener(btlistener);
b7=(Button)findViewById(R.id.bt7);
b7.setOnClickListener(btlistener);
b8=(Button)findViewById(R.id.bt8);
b8.setOnClickListener(btlistener);
b9=(Button)findViewById(R.id.bt9);
b9.setOnClickListener(btlistener);
b10=(Button)findViewById(R.id.bt_plus);
b10.setOnClickListener(btlistener);
b11=(Button)findViewById(R.id.bt_sub);
b11.setOnClickListener(btlistener);
b12=(Button)findViewById(R.id.bt_mul);
b12.setOnClickListener(btlistener);
b13=(Button)findViewById(R.id.bt_div);
b13.setOnClickListener(btlistener);
b14=(Button)findViewById(R.id.bt_equal);
b14.setOnClickListener(btlistener);
b15=(Button)findViewById(R.id.bt_dot);
b15.setOnClickListener(btlistener);
b16=(Button)findViewById(R.id.bt_cl);
b16.setOnClickListener(btlistener);
}
OnClickListener btlistener=new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_cl:
tv1.setText("0");
Data=0;
bBegin=true;
break;
case R.id.bt_plus:
case R.id.bt_sub:
case R.id.bt_mul:
case R.id.bt_div:
case R.id.bt_equal:
inputOperater(((Button)v).getText().toString());
break;
default:
inputNumber(((Button)v).getText().toString());
break;
}
Log.d("what",((Button)v).getText().toString());
}
};
@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;
}
private void inputNumber(String str){
if(bBegin)
tv1.setText(str);
else
tv1.append(str);
bBegin=false;
}
private void inputOperater(String opt){
try{
double temp=Double.parseDouble(tv1.getText().toString());
if(bBegin){
Operator=opt;
}else{
if(Operator.equals("=")){
Data=temp;
}else if(Operator.equals("+")){
Data+=temp;
}else if (Operator.equals("-")){
Data-=temp;
}else if(Operator.equals("*")){
Data*=temp;
}else if(Operator.equals("/")){
if(temp!=0){
Data/=temp;
}else{
throw new ArithmeticException();
}
}
tv1.setText(String.valueOf(Data));
Operator=opt;
}
}catch(NumberFormatException e){
tv1.setText(" Number Format Error!");
}catch(ArithmeticException e){
tv1.setText("Div Number CAN NOT BE ZERO!");
Operator="=";
}finally{
bBegin=true;
}
}
}
2.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#782566AA" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_weight="0.16"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:text="0"
android:textColor="#00f"
android:textSize="40sp" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/bt7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="40sp"
/>
<Button
android:id="@+id/bt8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="40sp"/>
<Button
android:id="@+id/bt9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="40sp"/>
<Button
android:id="@+id/bt_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="40sp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="40sp" />
<Button
android:id="@+id/bt5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="40sp"/>
<Button
android:id="@+id/bt6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="40sp"/>
<Button
android:id="@+id/bt_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="40sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="40sp"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="40sp"/>
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="40sp"/>
<Button
android:id="@+id/bt_mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="40sp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/bt_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textSize="40sp"/>
<Button
android:id="@+id/bt0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textSize="40sp"/>
<Button
android:id="@+id/bt_equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="40sp"/>
<Button
android:id="@+id/bt_div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="40sp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07" >
<Button
android:id="@+id/bt_cl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="c"
android:textSize="40sp"/>
</TableRow>
</TableLayout>
</LinearLayout>
3.執行結果

沒有留言:
張貼留言