Created
February 8, 2014 03:53
-
-
Save refiute/8876452 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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
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" > | |
<TextView | |
android:id="@+id/textView1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
android:gravity="center|right" | |
android:text="@string/result" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:textSize="50sp" /> | |
<LinearLayout | |
android:id="@+id/linearLayout2" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignLeft="@+id/linearLayout1" | |
android:layout_centerVertical="true" | |
android:gravity="center" > | |
<Button | |
android:id="@+id/Button4" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button4" /> | |
<Button | |
android:id="@+id/Button5" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button5" /> | |
<Button | |
android:id="@+id/Button6" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button6" /> | |
<Button | |
android:id="@+id/Times" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/times" /> | |
</LinearLayout> | |
<LinearLayout | |
android:id="@+id/linearLayout3" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignLeft="@+id/linearLayout2" | |
android:layout_below="@+id/linearLayout2" | |
android:layout_marginTop="19dp" | |
android:gravity="center" > | |
<Button | |
android:id="@+id/Button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button1" /> | |
<Button | |
android:id="@+id/Button2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button2" /> | |
<Button | |
android:id="@+id/Button3" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button3" /> | |
<Button | |
android:id="@+id/Divide" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/divide" /> | |
</LinearLayout> | |
<LinearLayout | |
android:id="@+id/linearLayout4" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignLeft="@+id/linearLayout3" | |
android:layout_below="@+id/linearLayout3" | |
android:layout_marginTop="17dp" | |
android:gravity="center" > | |
<Button | |
android:id="@+id/Button0" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button0" /> | |
<Button | |
android:id="@+id/Plus" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/plus" /> | |
<Button | |
android:id="@+id/Minus" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/minus" /> | |
<Button | |
android:id="@+id/Equal" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/equal" /> | |
</LinearLayout> | |
<LinearLayout | |
android:id="@+id/linearLayout1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_above="@+id/linearLayout2" | |
android:layout_alignLeft="@+id/textView1" | |
android:layout_marginBottom="15dp" | |
android:gravity="center" > | |
<Button | |
android:id="@+id/Button7" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button7" /> | |
<Button | |
android:id="@+id/Button8" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button8" /> | |
<Button | |
android:id="@+id/Button9" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/button9" /> | |
<Button | |
android:id="@+id/Clear" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="@string/clear" /> | |
</LinearLayout> | |
</RelativeLayout> |
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
package com.example.calculator; | |
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.TextView; | |
public class MainActivity extends Activity implements OnClickListener { | |
private int result = 0; | |
private int before_operator = -1; | |
private int value = 0; | |
private boolean wait = false; | |
private int[] Numbers = { R.id.Button0, R.id.Button1, R.id.Button2, | |
R.id.Button3, R.id.Button4, R.id.Button5, R.id.Button6, | |
R.id.Button7, R.id.Button8, R.id.Button9 }; | |
private int[] Operators = {R.id.Plus, R.id.Minus, R.id.Times, R.id.Divide}; | |
private void calculate(){ | |
switch(before_operator){ | |
case R.id.Plus: | |
result = value + result; | |
break; | |
case R.id.Minus: | |
result = value - result; | |
break; | |
case R.id.Times: | |
result = value * result; | |
break; | |
case R.id.Divide: | |
result = value / result; | |
break; | |
} | |
value = 0; | |
before_operator = -1; | |
wait = true; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
for(int i=0;i<10;i++){ | |
Button button = (Button)findViewById(Numbers[i]); | |
button.setOnClickListener(this); | |
} | |
for(int i=0;i<4;i++){ | |
Button button = (Button)findViewById(Operators[i]); | |
button.setOnClickListener(this); | |
} | |
Button clear = (Button)findViewById(R.id.Clear); | |
clear.setOnClickListener(new View.OnClickListener(){ | |
public void onClick(View v){ | |
result = 0; | |
before_operator = -1; | |
value = 0; | |
show(); | |
} | |
}); | |
Button equal = (Button)findViewById(R.id.Equal); | |
equal.setOnClickListener(new View.OnClickListener(){ | |
public void onClick(View v){ | |
calculate(); | |
show(); | |
} | |
}); | |
} | |
private void show(){ | |
TextView textview = (TextView)findViewById(R.id.textView1); | |
textview.setText(Integer.toString(result)); | |
} | |
@Override | |
public void onClick(View v) { | |
for(int i=0;i<10;i++){ | |
if(v.getId() == Numbers[i]){ | |
if(wait){ | |
wait = false; | |
result = 0; | |
} | |
result = result*10 + i; | |
show(); | |
return ; | |
} | |
} | |
if(before_operator != -1){ | |
calculate(); | |
show(); | |
} | |
before_operator = v.getId(); | |
value = result; | |
wait = true; | |
} | |
@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; | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="app_name">電卓</string> | |
<string name="action_settings">Settings</string> | |
<string name="result">0</string> | |
<string name="plus">+</string> | |
<string name="minus">-</string> | |
<string name="times">×</string> | |
<string name="divide">÷</string> | |
<string name="button0">0</string> | |
<string name="button1">1</string> | |
<string name="button2">2</string> | |
<string name="button3">3</string> | |
<string name="button4">4</string> | |
<string name="button5">5</string> | |
<string name="button6">6</string> | |
<string name="button7">7</string> | |
<string name="button8">8</string> | |
<string name="button9">9</string> | |
<string name="equal">=</string> | |
<string name="clear">C</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment