| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
                            Tags
                            
                        
                          
                          - 캡슐화
- Windows Forms
- c#
- 추천
- 표준 입출력
- 안드로이드 앱 개발
- 독립기념관
- 네트워크 프로그래밍
- 언제나휴일
- 알고리즘
- 프로젝트
- 소켓 통신
- 졸업 작품 소재
- 언제나 휴일
- c언어
- 유튜브 동영상 강의
- 소스 코드
- 실습으로 다지는 c#
- 산책하기 좋은 곳
- 실습
- 파이썬
- 동영상 강의
- 클래스 다이어그램
- 무료 동영상 강의
- 강의
- 표준 라이브러리 함수
- 동영상
- 원격 제어 프로그램
- C++
- 충남 천안
                            Archives
                            
                        
                          
                          - Today
- Total
프로그래밍 언어 및 기술 [언제나휴일]
4. 기본 컨트롤 – 7. ToggleButton 본문
이번에는 ToggleButton을 사용하는 간단한 실습을 합시다.

먼저 activity_main.xml 파일에 컨트롤을 배치합시다. 최상위 요소는 LinearLayout입니다. 그리고 자식으로 ToggleButton과 TextView를 배치하세요.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.ehclub.ex_togglebutton.MainActivity"
    android:orientation="vertical">
    <ToggleButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tb"
        android:textOff="꺼짐"
        android:textOn="켜짐"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textColor="#FF0000"
        android:id="@+id/tv"/>
</LinearLayout>이제 MainActivity.java 파일을 편집합시다. MainActivity 클래스에 ToggleButton을 참조할 멤버 필드를 선언하세요.
    ToggleButton tb;onCreate 메서드에서는 먼저 findViewById 메서드를 호출하여 xml 파일에 배치한 ToggleButton을 참조합니다.
        tb = (ToggleButton)findViewById(R.id.tb);그리고 클릭 리스너를 설정합니다. 리스너에서는 xml파일에 배치한 TextView 를 참조합니다. 그리고 ToggleButton의 isChecked 메서드를 호출하여 상태에 따라 TextView의 text 속성을 설정합니다.
        tb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv = (TextView)findViewById(R.id.tv);
                if(tb.isChecked()){
                    tv.setText("토클 버튼 상태 : 켜짐");
                }
                else{
                    tv.setText("토클 버튼 상태 : 꺼짐");
                }
            }
        });다음은 MainActivity.java 파일의 내용입니다.
package com.example.ehclub.ex_togglebutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
    ToggleButton tb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tb = (ToggleButton)findViewById(R.id.tb);
        tb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv = (TextView)findViewById(R.id.tv);
                if(tb.isChecked()){
                    tv.setText("토클 버튼 상태 : 켜짐");
                }
                else{
                    tv.setText("토클 버튼 상태 : 꺼짐");
                }
            }
        });
    }
}언제나휴일 추천 여행 및 산책
'Java 안드로이드 > 안드로이드' 카테고리의 다른 글
| 4. 기본 컨트롤 – 12. Custom ListView (0) | 2025.01.04 | 
|---|---|
| 4. 기본 컨트롤 – 11. ListView (0) | 2025.01.04 | 
| 4. 기본 컨트롤 – 10. SeekBar (0) | 2025.01.04 | 
| 4. 기본 컨트롤 – 8. ProgressBar (0) | 2025.01.04 | 
| 4. 기본 컨트롤 – 6. Switch (0) | 2025.01.04 | 
| 4. 기본 컨트롤 – 5. RadioButton (0) | 2025.01.04 | 
| 4. 기본 컨트롤 – 4. CheckBox (0) | 2025.01.04 | 
| 4. 기본 컨트롤 – 3. Button (0) | 2025.01.04 | 
 
                   
                   
                  