안슈 /3강 /for문 활용 2

버튼 누르면 1985 부터 2000까지 숫자 출력하기

 

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
<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
 
    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
 
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"> //스크롤 가능하도록 스크롤 뷰로 설정
 
 
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
 
    </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
package com.example.myapplication05;
 
 
 
public class MainActivity extends AppCompatActivity {
 
    Button bt;
    TextView tv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bt = findViewById(R.id.bt);//xml파일에 bt라는 아이디 가진애를 가져와서 bt변수에 담아라
        tv = findViewById(R.id.tv);
 
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { 
//bt 클릭하면 실행해라
 
                for(int i =1985; i <2001; i++){
//동안 계속 /(1985부터, 2001보다 작을때까지, 1씩 증가 하면서)/ 중괄호를 실행해라
//i를 1985부터 1씩 증가시키면서 2001보다 작을때까지 중괄호를 반복해라.
                tv.setText(tv.getText().toString()+ i+"\n" );
//tv의.텍스트를 정해라(tv에서.텍스트를 가져와서.스트링 타입으로 + i + "줄바꿈");
                
                }
            }
        });
    }
}
 
 
 

댓글

Designed by JB FACTORY