Android UI 위젯 활용법

Android에서는 다양한 UI 위젯을 제공하며, 이를 통해 사용자와의 상호작용을 구현할 수 있습니다. 예를 들어 스위치, 라디오 버튼, 체크박스 등이 있으며, 이번 문서에서는 이러한 위젯들의 기본적인 사용 방법을 배워보겠습니다.

먼저 간단한 레이아웃과 기능을 살펴보겠습니다:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView
        android:id="@+id/display_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="초기 텍스트" />
        
    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100" />
        
    <ToggleButton
        android:id="@+id/toggle_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="켜짐"
        android:textOff="꺼짐" />
        
    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <RadioButton
            android:id="@+id/radio_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="선택 1" />
            
        <RadioButton
            android:id="@+id/radio_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="선택 2" />
    </RadioGroup>
    
    <CheckBox
        android:id="@+id/check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="체크박스 선택" />
</LinearLayout>

다음은 위젯들을 제어하는 액티비티 코드입니다:

public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private ToggleButton toggleButton;
    private RadioGroup radioGroup;
    private RadioButton selectedRadioButton;
    private CheckBox checkBox;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekBar = findViewById(R.id.seek_bar);
        toggleButton = findViewById(R.id.toggle_button);
        radioGroup = findViewById(R.id.radio_group);
        checkBox = findViewById(R.id.check_box);
        textView = findViewById(R.id.display_text);

        // 시크바 이벤트 처리
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textView.setText("현재 값: " + progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });

        // 토글버튼 이벤트 처리
        toggleButton.setOnClickListener(v -> {
            if (toggleButton.isChecked()) {
                textView.append("\n토글버튼 켜짐");
            } else {
                textView.append("\n토글버튼 꺼짐");
            }
        });

        // 라디오 그룹 이벤트 처리
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            selectedRadioButton = findViewById(checkedId);
            textView.append("\n선택된 라디오 버튼: " + selectedRadioButton.getText());
        });

        // 체크박스 이벤트 처리
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                textView.append("\n체크박스 선택됨");
            } else {
                textView.append("\n체크박스 해제됨");
            }
        });
    }
}

다음으로 다이얼로그를 사용하는 방법도 알아보겠습니다. 아래는 간단한 다이얼로그 레이아웃입니다:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/input_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="입력하세요" />

    <Button
        android:id="@+id/confirm_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="확인" />
</LinearLayout>

다음은 다이얼로그를 표시하고 입력값을 처리하는 코드입니다:

public void showCustomDialog(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.custom_dialog, null);

    EditText inputField = dialogView.findViewById(R.id.input_field);
    Button confirmButton = dialogView.findViewById(R.id.confirm_button);

    builder.setView(dialogView);
    final AlertDialog dialog = builder.create();

    confirmButton.setOnClickListener(v -> {
        String userInput = inputField.getText().toString();
        if (!userInput.isEmpty()) {
            Toast.makeText(this, "입력된 값: " + userInput, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "값을 입력해주세요", Toast.LENGTH_SHORT).show();
        }
        dialog.dismiss();
    });

    dialog.show();
}

이외에도 메뉴나 알림창 등을 사용하여 더 복잡한 UI를 구성할 수 있습니다.

태그: Android UI.Widgets AlertDialog

6월 30일 01:56에 게시됨