setVisibility를 사용해보자

Posted by Hoit.
2015. 3. 27. 16:35 Study/Android

setVisibility를 사용해보자

 

setVisibility를 사용하면 버튼,텍스트뷰,이미지,레이아웃등 숨기거나 없애거나 다시보이게 하거나 가능

3가지를 사용할수 있다

①VISIBLE  보이게 한다

②INVISIBLE 가린다(공간차지)

③GONE  없앤다(공간차지x)   

②, ③번에 차이는 공간차지 여부이다

버튼2 클릭후 





 

사용방법 : btn.setVisibility(View.INVISIBLE); 이렇게 쓰면 된다

 

[string]

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="app_name">BtnStudy</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="btn1">1번 누르면 사라짐</string>
    <string name="btn2">2번 누르면 사라짐</string>
    <string name="btn3">3번 누르면 사라짐</string>
    <string name="btn4">4번 누르면 사라짐</string>
    <string name="btn5">초기화하고 버튼 VISIBLE</string>
 
</resources>
cs

[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
<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="com.hoitstudy.btn.BtnClick" >
 
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn1" />
 
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn2" />
 
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn3" />
 
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn4" />
    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn5" />
 
</LinearLayout>
cs

[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
70
71
72
73
74
75
76
package com.hoitstudy.btn;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.btnstudy.R;
 
public class BtnClick extends Activity 
{
    TextView tv;
    int i,j,k,l = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn1).setOnClickListener(mClickListener);
        findViewById(R.id.btn2).setOnClickListener(mClickListener);
        findViewById(R.id.btn3).setOnClickListener(mClickListener);
        findViewById(R.id.btn4).setOnClickListener(mClickListener);
        findViewById(R.id.btn5).setOnClickListener(mClickListener);
    }
 
    Button.OnClickListener mClickListener = new OnClickListener() 
    {
 
        @Override
        public void onClick(View v) 
        {
            switch(v.getId())
            {
            case R.id.btn1:
                i++;
                if(i==1)
                {
                    v.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.btn2:
                j++;
                if(j==2)
                {
                    v.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.btn3:
                k++;
                if(k==3)
                {
                    v.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.btn4:
                l++;
                if(l==4)
                {
                    v.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.btn5:
                findViewById(R.id.btn1).setVisibility(View.VISIBLE);
                findViewById(R.id.btn2).setVisibility(View.VISIBLE);
                findViewById(R.id.btn3).setVisibility(View.VISIBLE);
                findViewById(R.id.btn4).setVisibility(View.VISIBLE);
                i=0;
                j=0;
                k=0;
                l=0;
                break;
            }
        }
    };
}
cs

 

 

작성중................

 

 

'Study > Android' 카테고리의 다른 글

Toast(토스트) 출력  (0) 2015.07.30
버튼 클릭 이벤트  (0) 2015.03.26