简单的时间控件学习

package tw.android;



import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.*;



public class Main extends Activity {



    private DatePicker mDatePik;

    private TimePicker mTimePik;

    private TextView mTxtResult;

    private Button mBtnOK;



    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);



        setupViewComponent();

    }

    

    private void setupViewComponent() {

        // 从资源文件R中取得介面元件

        mDatePik = (DatePicker)findViewById(R.id.datePik);

        mTimePik = (TimePicker)findViewById(R.id.timePik);

        mTxtResult = (TextView)findViewById(R.id.txtResult);

        mBtnOK = (Button)findViewById(R.id.btnOK);



        mBtnOK.setOnClickListener(btnDoOKOnClick);

    }

    

    private Button.OnClickListener btnDoOKOnClick = new Button.OnClickListener() {

        public void onClick(View v) {

            String s = getString(R.string.result);

            mTxtResult.setText(s + mDatePik.getYear() + "年" +

                                  (mDatePik.getMonth()+1) + "月 " +

                                  mDatePik.getDayOfMonth() + "日" +

                                  mTimePik.getCurrentHour() + "點" +

                                  mTimePik.getCurrentMinute() + "分");

        }

    };

}

注意月份是要 +1的  (mDatePik.getMonth()+1) + "月 " 

你可能感兴趣的:(时间控件)