728x90

날짜를 선택하여 입력하는 EditText 예제이다.


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_marginTop="5dp"
        android:text="운행일자"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="@color/colorBlack"/>

    <EditText
        android:id="@+id/et_drivingDate"
        android:layout_width="120dp"
        android:layout_height="30dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="20dp"
        android:background="#E3E8EA"
        android:gravity="center"
        android:focusable="false"
        android:inputType="date"/>

</LinearLayout> 


public class MainActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    Context mContext;

    //Declaring EditText
    private EditText et_drivingDate;
    String drivingDate; // 운행일자
    final Calendar myCalendar = Calendar.getInstance();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        initView();
    }

    private void updateLabel() {
        String myFormat = "yyyy-MM-dd"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

        et_drivingDate.setText(sdf.format(myCalendar.getTime()));
    }

    private void initView() {
        //Initializing the views
        et_drivingDate = findViewById(R.id.et_drivingDate);
        DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }

        };
        et_drivingDate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new DatePickerDialog(MainActivity.this, date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

    }
}


728x90

'안드로이드 > Layout' 카테고리의 다른 글

LinearLayout 동적 생성  (0) 2020.10.01
SMS authentication with OTP Layout Example  (0) 2020.09.14
Floating Action Button  (0) 2020.07.11
CardView Layout 예제  (0) 2020.04.12
Meterial Design 로그인 Layout 예제  (0) 2020.03.21
블로그 이미지

Link2Me

,