안슈 /4강/for 문, Integer.parseint

입력한 숫자만큼 "안녕" 출력하기

 

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
<?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">
 
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"> //수평 정렬
 
        <EditText
            android:id="@+id/et"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="숫자를 입력하세요" //힌트메세지
            android:inputType="number"  //숫자만 입력가능하도록 !!
            />
 
        <Button
            android:id="@+id/bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
 
 
    </LinearLayout>
 
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">//스크롤 뷰
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
 
            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>
 
</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
package com.example.myapplication06;
 
 
 
public class MainActivity extends AppCompatActivity {
 
    TextView tv;
    EditText et;
    Button bt;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bt = findViewById(R.id.bt);
        et = findViewById(R.id.et);
        tv = findViewById(R.id.tv);
 
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                tv.setText("");
//tv 텍스트를 정해라 빈칸으로
 
                int m = Integer.parseInt(et.getText().toString());
//숫자 타입 m 에 담아라= Integer.parseInt-숫자 타입으로 바꿔라(et에서. 텍스트를 가져와서.문자(String)타입으로);
//et에서 문자를 가져와서 정수(숫자)타입으로 바꾸기
//문자 "1"과  숫자 "1"은 다름
 
                for (int i = 0; i < m; i++ ) {
//동안 (i가 0부터; i 가 m보다 작을때까지 ; i를 1씩 더하면서)
                    tv.setText(tv.getText().toString() + "안녕" + i + "\n");
//tv에. 텍스트를 정해라(tv에 텍스트를 얻어와서.문자타입으로 + "안녕" + i + "줄바꿈");
                }
                et.setText("");
//et에.정해라 텍스트를(빈칸으로);
 
            }
        });
    }
}
 
 

 

댓글

Designed by JB FACTORY