在这里要学习的东西,以前学过,但这次来学的话,好像思路特别的清晰。
主要学到的知识点有:
1、SimpleDateFormat
//SimpleDateFormat初始日期/时间格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//显示当前日期
showDialog("当前日期",sdf.format(new Date()));
SimpleDateFormatmpleDateFormat函数语法:
G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时 在上午或下午 (0~11) z 时区
2、AlertDialog和AlertDialog.Builder
a) AlertDialog类是Dialog类的子类。它默认提供了3个按钮和一个文本消息。这些按钮可以按需要来使他们显示或隐藏。
b) AlertDialog.Builder: AlertDialog类中有一个内部类,名为‘Builder’,Builder类提供了为对话框添加多选或单选列表,以及为这些列表添加事件处理的功能。另外,这个Builder类将AlertDialog对话框上的3个按钮按照他们的位置分别称呼为:PositiveButton, NeutralButton, NegativeButton
============================分隔线==============================
main.xml代码
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>android:id="@+id/btnShowDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show current date" />android:id="@+id/btnShowTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show current time" />
DateTime.java代码
package com.js.datetime; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class DateTime extends Activity implements OnClickListener {
Button btnShowDate;
Button btnShowTime;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main);
btnShowDate = (Button)findViewById(R.id.btnShowDate);
btnShowTime = (Button)findViewById(R.id.btnShowTime);
btnShowDate.setOnClickListener(this);
btnShowTime.setOnClickListener(this); }
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnShowDate:
//SimpleDateFormat初始日期/时间格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//显示当前日期
showDialog("当前日期",sdf.format(new Date()));
break;
case R.id.btnShowTime:
SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
//显示当前时间
showDialog("当前时间",sdf1.format(new Date()));
break;
}
}
private void showDialog(String string, String format)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框的图标
builder.setIcon(android.R.drawable.ic_dialog_info);
//设置对话框的标题
builder.setTitle(string);
//设置对话框的内容
builder.setMessage(format);
//设置对话框的按钮
builder.setPositiveButton("确定", null);
//显示对话框
builder.create().show();
Intent intent;
} }