안드로이드 스튜디오 간단한 계산기 만들기 ( Button Click기능 )

2019. 12. 23. 21:592020/Android App Develop

클래스에 더하기, 빼기, 곱하기, 나누기 함수를 생성 => xml에서 Button component에 각 버튼별로 함수를 지정

 

<activity_practice.xml>

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PracticeActivity">
 
 
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Cacluator"
            android:textSize="20dp"
            android:textColor="@color/Blue"
            android:gravity="center"
            android:layout_marginBottom="5dp"></TextView>
        <EditText
            android:id="@+id/editText_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Number 1"
 
             />
 
        <EditText
            android:id="@+id/editText_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Number 2"
            android:ems="10"
 
             />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
 
            <Button
                android:id="@+id/addButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:onClick="addButton"
                android:text="ADD" />
 
            <Button
                android:id="@+id/MinusButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="minusButton"
                android:text="MINUS" />
 
            <Button
                android:id="@+id/MultiplyButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="multiplyButton"
                android:text="MULTIPLY" />
 
            <Button
                android:id="@+id/DivideButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="divideButton"
                android:text="DIVIDE" />
        </LinearLayout>
 
        <TextView
            android:id="@+id/textView_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="@color/Red"
            ></TextView>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

<PracticeActivity.java>

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.myapplication;
 
 
 
public class PracticeActivity extends AppCompatActivity {
 
    public EditText editText_1;
    public EditText editText_2;
    public Button button;
    public TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
 
}
 
 
    public void addButton(View view){
        editText_1 = findViewById(R.id.editText_1);
        editText_2 = findViewById(R.id.editText_2);
        textView = findViewById(R.id.textView_result);
        button = findViewById(R.id.addButton);
 
        int number_1 = Integer.parseInt(editText_1.getText().toString());
        int number_2 = Integer.parseInt(editText_2.getText().toString());
        textView.setText(Integer.toString(number_1+number_2));
    }
    public void minusButton(View view){
        editText_1 = findViewById(R.id.editText_1);
        editText_2 = findViewById(R.id.editText_2);
        textView = findViewById(R.id.textView_result);
        button = findViewById(R.id.addButton);
 
        int number_1 = Integer.parseInt(editText_1.getText().toString());
        int number_2 = Integer.parseInt(editText_2.getText().toString());
        textView.setText(Integer.toString(number_1-number_2));
    }
 
    public void multiplyButton(View view){
        editText_1 = findViewById(R.id.editText_1);
        editText_2 = findViewById(R.id.editText_2);
        textView = findViewById(R.id.textView_result);
        button = findViewById(R.id.addButton);
 
        int number_1 = Integer.parseInt(editText_1.getText().toString());
        int number_2 = Integer.parseInt(editText_2.getText().toString());
        textView.setText(Integer.toString(number_1*number_2));
    }
    public void divideButton(View view){
        editText_1 = findViewById(R.id.editText_1);
        editText_2 = findViewById(R.id.editText_2);
        textView = findViewById(R.id.textView_result);
        button = findViewById(R.id.addButton);
 
        int number_1 = Integer.parseInt(editText_1.getText().toString());
        int number_2 = Integer.parseInt(editText_2.getText().toString());
        textView.setText(Integer.toString(number_1/number_2));
    }
 
 
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

Integer.parseInt( String ) : String -> Int

Integer.toString( Int ) : Int -> String