안드로이드 계산기앱 (TableLayout 만든거)
[activity_main.xml]
<?xml version="1.0" encoding="utf-8"?>
<TableLayout --생략--android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:padding="10dp"
tools:context="com.example.lhcdesktop.myapplication.MainActivity">
<TableRow>
<EditText
android:id="@+id/edt1"
android:layout_span="5"
android:hint="숫자1입력"/>
</TableRow>
<TableRow>
<EditText
android:id="@+id/edt2"
android:layout_span="5"
android:hint="숫자2입력"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn0"
android:text="0"/>
<Button
android:id="@+id/btn1"
android:text="1"/>
<Button
android:id="@+id/btn2"
android:text="2"/>
<Button
android:id="@+id/btn3"
android:text="3"/>
<Button
android:id="@+id/btn4"
android:text="4"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn5"
android:text="5"/>
<Button
android:id="@+id/btn6"
android:text="6"/>
<Button
android:id="@+id/btn7"
android:text="7"/>
<Button
android:id="@+id/btn8"
android:text="8"/>
<Button
android:id="@+id/btn9"
android:text="9"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnplus"
android:layout_span="5"
android:text="더하기"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnminus"
android:layout_span="5"
android:text="빼기"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btndivide"
android:layout_span="5"
android:text="나누기"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btngobhagi"
android:layout_span="5"
android:text="곱하기"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnclear"
android:layout_span="5"
android:text="clear"
/>
</TableRow>
<TableRow>
<TextView
android:textColor="#ff0000"
android:layout_span="5"
android:id="@+id/txtV"
android:text="계산 결과 : "
android:textSize="25dp"
android:background="#ffe100"/>
</TableRow>
</TableLayout>
[MainActivity.java]
--생략-- public class MainActivity extends AppCompatActivity {
EditText edtNum1,edtNum2;
Button[] buttonNum=new Button[10];
Button btnAdd,btnMul,btnMinus,btnDiv,btnClear,btngobhagi;
TextView txtResult;
Integer[] btnID={R.id.btn0,R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,
R.id.btn5,R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9};
String num1,num2;
Integer result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("간단한계산기");
txtResult=(TextView)findViewById(R.id.txtV);
edtNum1=(EditText)findViewById(R.id.edt1);
edtNum2=(EditText)findViewById(R.id.edt2);
btnAdd=(Button)findViewById(R.id.btnplus);
btnMinus=(Button)findViewById(R.id.btnminus);
btnDiv=(Button)findViewById(R.id.btndivide);
btngobhagi=(Button)findViewById(R.id.btngobhagi);
btnClear=(Button)findViewById(R.id.btnclear);
//숫자버튼 초기화 (연속처리하기)
for(int i=0;i<buttonNum.length;i++){
buttonNum[i]=(Button)findViewById(btnID[i]);
}
for(int i=0;i<buttonNum.length;i++){
final int index=i;
buttonNum[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(edtNum1.isFocused()){
num1=edtNum1.getText().toString()+buttonNum[index].getText().toString(); //에러남 ..(지역변수라.. 지역변수는 사용하고 끝나면 없어짐... 그래서 final함수를 사용한거야!!)
edtNum1.setText(num1);
}else if(edtNum2.isFocused()){
num2=edtNum2.getText().toString()+buttonNum[index].getText().toString();
edtNum2.setText(num2);
}else{
Toast.makeText(getApplicationContext(),"숫자를 입력하세여",Toast.LENGTH_LONG).show();
}
}
});
}
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1=edtNum1.getText().toString();
num2=edtNum2.getText().toString();
result=Integer.parseInt(num1)+Integer.parseInt(num2);
txtResult.setText("계산 결과 : "+result.toString());
}
});
btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1=edtNum1.getText().toString();
num2=edtNum2.getText().toString();
result=Integer.parseInt(num1)-Integer.parseInt(num2);
txtResult.setText("계산 결과 : "+result.toString());
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1=edtNum1.getText().toString();
num2=edtNum2.getText().toString();
result=Integer.parseInt(num1)/Integer.parseInt(num2);
txtResult.setText("계산 결과 : "+result.toString());
}
});
btngobhagi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1=edtNum1.getText().toString();
num2=edtNum2.getText().toString();
result=Integer.parseInt(num1)*Integer.parseInt(num2);
txtResult.setText("계산 결과 : "+result.toString());
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edtNum1.setText("");
edtNum2.setText("");
txtResult.setText("계산 결과 : ");
}
});
}
}
'■ Android > Practice' 카테고리의 다른 글
안드로이드 회원가입 화면 (0) | 2019.11.22 |
---|---|
안드로이드 로그인 화면 (0) | 2019.11.21 |
안드로이드 다이얼로그(AlertDialog) (0) | 2018.12.17 |
중첩 리니어레이아웃(LinearLayout) (0) | 2018.10.22 |
안드로이드 주문 (0) | 2018.10.17 |