Date&Time

4485 단어
코드:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/linearLayout">

            <DatePicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/datePicker"
                android:layout_gravity="center_horizontal" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button"
                android:layout_gravity="center_horizontal" />

            <TimePicker
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/timePicker"
                android:layout_gravity="center_horizontal" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button2"
                android:layout_gravity="center_horizontal" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

MainActivity:
package com.example.hailang.date_time;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private DatePicker datePicker;
    private TimePicker timePicker;
    private Button btn1;
    private Button btn2;

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

        datePicker = (DatePicker)findViewById(R.id.datePicker);
        btn1 = (Button)findViewById(R.id.button);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder date = new StringBuilder("          :");
                date.append(datePicker.getYear())
                    .append("-")
                    .append(datePicker.getMonth())
                    .append("-")
                    .append(datePicker.getDayOfMonth());
                btn1.setText(date.toString());
            }
        });

        timePicker = (TimePicker)findViewById(R.id.timePicker);
        btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder time = new StringBuilder("          :");
                time.append(timePicker.getCurrentHour())
                    .append(":")
                    .append(timePicker.getCurrentMinute());
                btn2.setText(time.toString());
            }
        });
    }
}

좋은 웹페이지 즐겨찾기