안슈/2강/if문활용 1

텍스트 박스에 나이를 입력하고

버튼 누르면

[유아 초 중 고 성인] 중 하나 출력

 

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
 
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_editor_absoluteX="41dp"
        tools:layout_editor_absoluteY="19dp" />
 
    <EditText
 
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"//숫자만 쓰도록 강제
        tools:layout_editor_absoluteX="43dp"
        tools:layout_editor_absoluteY="69dp" />
 
    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="42dp"
        tools:layout_editor_absoluteY="148dp" />
 
</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
46
47
48
49
50
51
package com.example.myapplication01;
 
 
 
public class MainActivity extends AppCompatActivity {
 
    Button bt;
    EditText et;
    TextView tv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bt = (Button) findViewById(R.id.bt);
        et = (EditText) findViewById(R.id.et);
        tv = (TextView) findViewById(R.id.tv);
 
        tv.setText("나이를 입력하세요");
 
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String g = et.getText().toString();
                int h = Integer.parseInt(g);
                if (h < 8) {
                    tv.setText("유아");
                } else if (h < 14) {
                    tv.setText("초등");
                } else if (h < 17) {
                    tv.setText("중학");
                } else if (h < 20) {
                    tv.setText("고등");
                } else {
                    tv.setText("성인");
                }//
 
            }
 
        });
    }
}
 
 
 
 
자바 코드 설명
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
package com.example.myapplication01;
 
 
 
public class MainActivity extends AppCompatActivity {
 
    Button bt; //bt는 버튼 타입 변수다! 선언
    EditText et; //et는 에딧텍스트 타입 변수다!
    TextView tv;//tv는 텍뷰 타입 변수다!
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bt = (Button) findViewById(R.id.bt);//xml에 bt아이디 가진놈을 찾아서 /bt변수에 입력
        et = (EditText) findViewById(R.id.et);//xml에 et아이디 가진놈을 찾아서 /et변수에 입력
        tv = (TextView) findViewById(R.id.tv);//xml에 tv아이디 가진놈을 찾아서 /tv변수에 입력
 
        tv.setText("나이를 입력하세요");//tv의 텍스트를 셋팅해라 ("나이를 입력하세요")로
 
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //bt클릭하면 중괄호를 실행해라
 
                String g = et.getText().toString();
//et에서. 텍스트를 받아서 .스트링 타입으로 / 스트링 타입 변수 g에 넣어라
                int h = Integer.parseInt(g);
//g를 가져와서 강제로 int 타입으로 바꾸고 그걸 h변수에 넣어라
                if (h < 8) {
                    tv.setText("유아");  //만약 h가 8보다 작으면 {tv의 텍스트를 "유아"로 정해라}
                } else if (h < 14) {
                    tv.setText("초등");  //아니고 만약 h가 14보다 작으면 {tv의 텍스트를 "초등"으로 정해라}
                } else if (h < 17) {
                    tv.setText("중학");  //아니고 만약 h가 17보다 작으면 {tv의 텍스트를 "중학"으로 정해라}
                } else if (h < 20) {
                    tv.setText("고등");  //아니고 만약 h가 20보다 작으면{tv의 텍스트를 "성인"으로 정해라}
                } else {
                    tv.setText("성인");  //아니면 {tv 텍스트를 "성인"으로 정해라}
                }//
 
            }
 
        });
    }
}
 
 
 

 

 

댓글

Designed by JB FACTORY