Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Tags
more
Archives
Today
Total
관리 메뉴

재훈재훈

고객 정보 입력 화면 만들기 예제 본문

Computer Engineering/Android

고객 정보 입력 화면 만들기 예제

jaehoonx2 2018. 4. 15. 16:52

참고 - Picker 가이드

https://developer.android.com/guide/topics/ui/controls/pickers.html


activity_main.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
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/nametag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="55dp"
        android:layout_marginTop="100dp"
        android:text="이       름"
        android:textSize="20dp" />
 
    <TextView
        android:id="@+id/agetag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="55dp"
        android:layout_marginTop="200dp"
        android:text="나       이"
        android:textSize="20dp" />
 
    <TextView
        android:id="@+id/birthdaytag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/nametag"
        android:layout_marginBottom="200dp"
        android:text="생년월일"
        android:textSize="20dp" />
 
    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_alignBaseline="@+id/nametag"
        android:layout_toEndOf="@+id/birthdaytag"
        android:hint="홍길동"
        android:inputType="text" />
 
    <EditText
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/agetag"
        android:layout_toEndOf="@+id/birthdaytag"
        android:ems="10"
        android:hint="25"
        android:inputType="number" />
 
    <Button
        android:id="@+id/birthday"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/birthdaytag"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-200dp"
        android:onClick="onBirthdayClicked" />
 
    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:text="저장" />
 
</RelativeLayout>
 
cs


MainActivity.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
77
78
package com.tistory.jaehoonx2.doitmission_07;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
***********고객 정보 입력 화면 만들기 예제***********
    고객의 이름, 나이, 생년월일을 입력 받는 예제
    이름과 나이를 입력받는 입력상자, 생년월일을 표시하는 버튼, 저장 버튼으로 구성됨.
    이름은 텍스트, 나이는 숫자로, 각각 타입에 맞는 키패드를 띄움
    나이 입력상자의 경우, 최대 3자리까지만 입력할 수 있음
    생년월일 버튼의 경우, 오늘 날짜로 초기화 되어 있음
    생년월일 버튼을 클릭할 경우, 날짜 선택창이 나타남
    날짜를 선택하면 버튼 텍스트가 해당 날짜로 바뀜
    저장 버튼을 누르면 토스트로 고객이 입력한 정보를 순서대로 띄어줌
 */
public class MainActivity extends AppCompatActivity {
 
    EditText name;      // 이름 입력상자
    EditText age;       // 나이 입력상자
    Button birthday;    // 생년월일 입력상자(instance variable)
                        // 생년월일 입력상자의 경우 프래그먼트에서도 접근해야 하기 때문에
                        // onCreate 내 지역변수가 아닌, 클래스 영역에 선언을 함.
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        name = (EditText) findViewById(R.id.name);
        age = (EditText) findViewById(R.id.age);
        age.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                InputFilter[] filters = new InputFilter[1];             // age의 입력 최대길이 제한
                filters[0= new InputFilter.LengthFilter(3);
                age.setFilters(filters);
            }
 
            @Override
            public void afterTextChanged(Editable s) {}
        });
 
        Date today = new Date();      // birthday 버튼의 초기화를 위해 date 객체와 SimpleDataFormat 사용
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy년 MM월 dd일");
        String result = dateFormat.format(today);
 
        birthday = (Button) findViewById(R.id.birthday);
        birthday.setText(result);       // 오늘 날짜로 birthday 버튼 텍스트 초기화
 
        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {        // 저장 버튼을 클릭하면 토스트로 고객 정보를 띄워주기
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "이름 : " + name.getText().toString(), Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "나이 : " + age.getText().toString(), Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "생년월일 : " + birthday.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });
    }
 
    public void onBirthdayClicked (View v) {
        android.support.v4.app.DialogFragment newFragment = new DatePickerFragment();   //DatePickerFragment 객체 생성
        newFragment.show(getSupportFragmentManager(), "datePicker");                //프래그먼트 매니저를 이용하여 프래그먼트 보여주기
    }
 
}
cs


DatePickerFragment.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
package com.tistory.jaehoonx2.doitmission_07;
 
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
 
import java.util.Calendar;
 
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
 
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();      // 오늘 날짜로 디폴트값을 설정하기 위해 캘린더 객체 선언
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);              // MONTH : 0~11
        int day = c.get(Calendar.DAY_OF_MONTH);
 
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day); // this는 리스너를 가르키는데 이 프래그먼트 클래스 자신을 가리킨다.
    }
 
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        MainActivity activity = (MainActivity) getActivity();              // MainActivity의 birthday 버튼에 접근하기 위해 액티비티 객체 선언
        activity.birthday.setText(year+"년 "+(month+1)+"월 "+ day + "일"); // 유저가 선택한 날짜로 버튼 텍스트 변경
    }
}
cs




동작 화면