공부하는 블로그

(안드로이드 프로그래밍) 미니 계산기 만들기 실습 본문

Develop/안드로이드

(안드로이드 프로그래밍) 미니 계산기 만들기 실습

모아&모지리 2017. 9. 26. 22:13

(완성)



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ktds.lesson04.MainActivity">

<EditText
android:id="@+id/et_f_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자를 입력하세요"
android:inputType="number"/>
<EditText
android:id="@+id/et_s_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자를 입력하세요"
android:inputType="number"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#777777"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_summit"
android:text="계산결과보기"/>
</LinearLayout>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ktds.lesson04;
 
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends AppCompatActivity {
 
    private EditText et_f_num;
    private EditText et_s_num;
    private Button  btn_summit;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        et_f_num = (EditText) findViewById(R.id.et_f_num);
        et_s_num = (EditText) findViewById(R.id.et_s_num);
        btn_summit = (Button ) findViewById(R.id.btn_summit);
 
        btn_summit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                int fNum = Integer.parseInt( et_f_num.getText().toString() );
                int sNum = Integer.parseInt( et_s_num.getText().toString() );
 
                String message = String.format(" %d + %d = %d" , fNum, sNum, fNum+sNum );
 
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("계산결과");
                dialog.setMessage(message);
                dialog.setPositiveButton("확인"null);
                dialog.create().show();
 
            }
        });
 
    }
}
 
cs