본문 바로가기
학교 강의/안드로이드 교양

안드교양 10주차

by hoshi03 2023. 11. 9.

xml에서 시간, 날짜 등 디자인하기

 

CalendarView <- 달력

 

TimePicker <- 알람 설정하는 그거

 

DatePicker <- 달력 고르는 위젯

 

Chronometer <- 스탑워치 느낌으로 사용함, 텍스트를  넣을떄 format 기능을 이용

 

사용 예시

<?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=".MainActivity"
    android:orientation="vertical">

    <Chronometer
        android:text="달리기 기록"
        android:format="달리기 기록%s"
        android:textSize="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

    <DigitalClock
        android:textSize="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <AnalogClock
        android:layout_width="100dp"
        android:layout_height="100dp"/>

        <TimePicker
            android:timePickerMode="spinner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</LinearLayout>

 

 

실습 - 스톱워치, 날짜 저장, !자동완성 <- 매우 중요할 것 같다, 2글자정도 단어를 입력하면 자동으로 완성이 된다

 

package com.example.aap;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.Chronometer;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

    Button btnStart, btnend;
    CalendarView cal;
    TimePicker timepicker;
    RadioButton rdocal, rdotime;
    TextView tvyear, tvmonth, tvday, tvhour, tvmin;
    Chronometer chro;
    Integer year1, month1, day1;
    AutoCompleteTextView autosingle;

    //자동완성 단어 목록 배열
    String[] str = {"hy_공대", "hy_미대", "hy_음대", "friday", "friend"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = findViewById(R.id.btnStart);
        timepicker = findViewById(R.id.timepiker);
        cal = findViewById(R.id.cal);
        rdocal = findViewById(R.id.rdocal);
        rdotime = findViewById(R.id.rdotime);
        chro = findViewById(R.id.chro);
        btnend = findViewById(R.id.btnEnd);
        tvyear = findViewById(R.id.tvyear);
        tvmonth = findViewById(R.id.tvmonth);
        tvday = findViewById(R.id.tvday);
        tvhour= findViewById(R.id.tvhour);
        tvmin = findViewById(R.id.tvmin);
        autosingle = findViewById(R.id.autosingle);

        //자동완성
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this,
                android.R.layout.simple_dropdown_item_1line, str);
        autosingle.setAdapter(adapter);


        cal.setVisibility(View.INVISIBLE);
        timepicker.setVisibility(View.INVISIBLE);


        rdocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cal.setVisibility(View.VISIBLE);
                timepicker.setVisibility(View.INVISIBLE);
            }
        });

        rdotime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cal.setVisibility(View.INVISIBLE);
                timepicker.setVisibility(View.VISIBLE);
            }
        });

       cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
           @Override
           public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {
               year1 = year;
               month1 = month;
               day1 = day;
           }
       });

        btnStart.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                chro.setBase(SystemClock.elapsedRealtime());
                chro.start();
                chro.setTextColor(Color.RED);
            }
        });

        btnend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chro.stop();
                chro.setTextColor(Color.GREEN);
                tvyear.setText(Integer.toString(year1));
                tvmonth.setText(Integer.toString(month1));
                tvday.setText(Integer.toString(day1));
                tvhour.setText(Integer.toString(timepicker.getHour()));
                tvmin.setText(Integer.toString(timepicker.getMinute()));
            }
        });



    }
}

 

<?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=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <AutoCompleteTextView
            android:id="@+id/autosingle"
            android:completionThreshold="2"
            android:textSize="30dp"
            android:hint="auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Chronometer
            android:id="@+id/chro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:format="예약에 걸린 시간%s"
            android:textSize="20dp" />
        <Button
            android:id="@+id/btnStart"
            android:text="시작"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <RadioGroup
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rdocal"
            android:text="Calender"
            android:textSize="20dp"
            android:paddingLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/rdotime"
            android:text="timePicker"
            android:textSize="20dp"
            android:paddingLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="300dp">
            <CalendarView
                android:id="@+id/cal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:showWeekNumber="false"/>
            <TimePicker
                android:id="@+id/timepiker"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:timePickerMode="spinner" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnEnd"
                android:text="종료"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/tvyear"
                android:text="0000"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="년"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tvmonth"
                android:text="00"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="월"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tvday"
                android:text="00"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="일"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tvhour"
                android:text="00"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="시"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tvmin"
                android:text="00"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="분"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>


    </LinearLayout>
</LinearLayout>

 

 

 

스크롤뷰, 교재 참고해서 보기

'학교 강의 > 안드로이드 교양' 카테고리의 다른 글

안드교양 12주차  (2) 2023.11.23
안드교양 11주차  (0) 2023.11.16
시험대비  (1) 2023.10.25
안드교양 7주차 레이아웃, 시험범위  (0) 2023.10.19
7주차, 시험  (0) 2023.10.12