Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- springboot
- podinit
- 리눅스
- viewmodel
- calendar
- PostgreSQL
- addTextChangedListner
- prolificinteractive/material-calendarview
- Button
- RestAPI
- Dialog
- livedata
- editText
- DialogFragment
- cocoapod
- backgroundTint
- android
Archives
- Today
- Total
코코딩딩
[안드로이드/android] customdialog, timepicker 본문
사용자의 시간을 입력받기 위해 customdialog에 timepicker를 사용해 작업을 진행하였다.
customdialog 레이아웃 만들기
기존에 사용했던 customdialog는 DialogFragment를 상속받아 만들었지만
이번에는 레이아웃 파일만 따로 만들고 다음과 같이 레이아웃 파일을 setContenView를 해주면 자신이 원하는 디자인의 dialog를 사용할 수 있다.
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_time_select_layout);
dialog_time_select_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_round"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:padding="10dp"
android:orientation="vertical">
<TimePicker
android:id="@+id/timePicker"
android:layout_gravity="center"
android:layout_width="350dp"
android:scrollbarSize="200dp"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
<Button
android:layout_gravity="center"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="@drawable/bt_primary"
android:id="@+id/ok_btn"
android:textColor="#ffffff"
android:text="선택"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
사용자의 취침 시간과 기상시간을 기록하기 위한 기능이며 버튼을 누르면 dialog를 띄우는 메서드를 만들었다.
dialog창이 열리면 activity의 배경이 회색으로 변하는데 이를 투명하게 하는 코드 또한 추가하였다.
나머지 메서드는 시간처리를 위한 것인데 timepicker의 경우 int로 시간과 분을 받아올 수 있다.
그런데 숫자가 10보다 작을 경우 앞에 0이 없어 빈자리를 매꾸기 위해 0을 추가하였고 12 이상의 오후 숫자는 -12를 한 후 edittext에 오후가 입력되게 구현하였다.
package com.example.timedialogfinish;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TimePicker;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.example.timedialogfinish.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private boolean isAfter = false;
private TimePicker timePicker;
private int user_select_sleepTime_hour=22;
private int user_select_sleepTime_minute=0;
private int user_select_awakeTime_hour=6;
private int user_select_awakeTime_minute=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
//취침시간 고르는 버튼
binding.targetSleepBtn.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
isAfter = false;
timeDialogShow(isAfter);
}
});
//기상시간 고르는 버튼
binding.targetAwakeBtn.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
isAfter = true;
timeDialogShow(isAfter);
}
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
public void timeDialogShow(boolean isAfterr){
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_time_select_layout);
timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
// 디폴트 시간 설정
if(isAfterr){
//기상
timePicker.setHour(user_select_awakeTime_hour);
timePicker.setMinute(user_select_awakeTime_minute);
}
else{
//취침
timePicker.setHour(user_select_sleepTime_hour);
timePicker.setMinute(user_select_sleepTime_minute);
}
// 유저가 선택한 시간이 입력되는 버튼
dialog.findViewById(R.id.ok_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isAfterr){
//기상
//et에 출력할 시간 ex) 오전/오후 7:00
binding.targetAwake.setText(timeToStringEt(timePicker.getHour(),timePicker.getMinute()));
}else{
//취침
//et에 출력할 시간 ex) 오전/오후 7:00
binding.targetSleep.setText(timeToStringEt(timePicker.getHour(),timePicker.getMinute()));
}
//창 닫기
dialog.dismiss();
}
});
//다이얼로그 시작하기 위한 코드
// dialog 배경 투명
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// dialog 이외 화면 투명
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.show();
}
public String timeToString(int time){
String result = "";
if(time<10){
result = "0"+Integer.toString(time);
}else{
result = Integer.toString(time);
}
return result;
}
public String timeToStringEt(int hour,int minute){
String result = "";
int re_hour = 0;
// 13시 부터는 12를 빼서 1시 이고 오후로 표시
if(hour>12){
re_hour = hour-12;
result = "오후"+timeToString(re_hour)+":"+timeToString(minute);
}else{
re_hour = hour;
result = "오전"+timeToString(re_hour)+":"+timeToString(minute);
}
return result;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="64dp">
<TextView
android:layout_width="match_parent"
android:layout_height="24dp"
android:text="취침시간"
android:textSize="16sp"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="top">
<GridLayout
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<EditText
android:id="@+id/target_sleep"
android:layout_width="match_parent"
android:layout_height="46dp"
android:textSize="16sp"
android:inputType="text"
android:enabled="false"
android:textColor="@color/black"
android:imeOptions="actionDone"
android:layout_column="1"
android:hint="취침시간"/>
<ImageButton
android:id="@+id/target_sleep_btn"
android:layout_width="20dp"
android:layout_height="40dp"
android:layout_column="1"
android:layout_gravity="right"
android:layout_marginRight="6dp"
android:src="@mipmap/btn_next"
/>
</GridLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="24dp"
android:text="기상시간"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginTop="32dp"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<EditText
android:id="@+id/target_awake"
android:layout_width="match_parent"
android:layout_height="46dp"
android:textSize="16sp"
android:inputType="text"
android:enabled="false"
android:textColor="@color/black"
android:imeOptions="actionDone"
android:layout_column="1"
android:hint="기상시간"/>
<ImageButton
android:id="@+id/target_awake_btn"
android:layout_width="20dp"
android:layout_height="40dp"
android:layout_column="1"
android:layout_gravity="right"
android:layout_marginRight="6dp"
android:src="@mipmap/btn_next"
/>
</GridLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="64dp">
<Button
android:id="@+id/lay6nextbtn"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@drawable/bt_primary"
android:gravity="center"
android:text="완료"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
databinding을 이용하고 있어 이용하지 않을 경우 가장 바깥에 있는 <layout>을 지운 후 사용하면 된다.
화면
'일단기록 > 매일기록' 카테고리의 다른 글
[안드로이드/android] recyclerView (0) | 2022.04.28 |
---|---|
[안드로이드/android] 카카오api 카카오링크(v2-link) 메세지 전송 (1) | 2022.04.27 |
[리눅스] tomcat, apache 설치하기 (0) | 2022.04.25 |
[리눅스] tomcat service 등록하기 (0) | 2022.04.22 |
[리눅스] postgresql-12 설치하기 (기본 경로 변경) (0) | 2022.04.22 |