相信对于手机的时间日期设置大家一定都不陌生吧,今天举一个关于时间日期设置的示例,其中有些许不完善之处,例如如何使设置的时间日期和手机系统同步等。感兴趣的读者可以根据自身经验加以完善。
现来看看具体示例,希望对大家有所帮助。
首先是时间设置:
.java文件(MainActivity.java)代码如下:
package com.example.activity_time_date; import java.util.Calendar; import android.app.Activity; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class MainActivity extends Activity { private TextView mytext = null; private Button mybutton1 = null; private Button mybutton2 = null; private int mHour; private int mMinute; static final int TIME_DIALOG_ID = 0; private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub mHour = hourOfDay; mMinute = minute; updateDisplay(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mytext = (TextView)findViewById(R.id.textview); mybutton1 = (Button)findViewById(R.id.button1); mybutton2 = (Button)findViewById(R.id.button2); mybutton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(TIME_DIALOG_ID); } }); mybutton2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MainActivity.this.startActivity(new Intent(MainActivity.this, dateActivity.class)); } }); final Calendar cal = Calendar.getInstance(); mHour = cal.get(Calendar.HOUR_OF_DAY); mMinute = cal.get(Calendar.MINUTE); updateDisplay(); } private void updateDisplay(){ mytext.setText(new StringBuilder().append(pad(mHour)).append(":") .append(pad(mMinute))); } private static String pad(int i){ if (i >= 10) return String.valueOf(i); else return "0" + String.valueOf(i); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); } return null; } }
布局文件(activity_main.xml)代码如下:
<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" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="设置时间" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下一页" /> </LinearLayout> </RelativeLayout>
运行效果如下图所示:
日期设置和时间设置基本一致,在此不再赘述。读者可以调试并改进本文示例代码,相信会有新的收获!