给大家入门的小例子
java文件:[java]
package com.example.mydatetime;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowDate = (Button) findViewById(R.id.btnShowDate);
Button btnShowTime = (Button) findViewById(R.id.btnShowTime);
Button btnActivity = (Button) findViewById(R.id.btnActivity);
btnShowDate.setOnClickListener(this);
btnShowTime.setOnClickListener(this);
btnActivity.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void showDialog(String title, String msg){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton("确定", null);
builder.create().show();
Intent intent;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnShowDate:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
showDialog("当前日期",sdf.format(new Date()));
break;
case R.id.btnShowTime:
SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
showDialog("当前时间", sdfTime.format(new Date()));
break;
case R.id.btnActivity:
Intent intent = new Intent(this, TabActivity01.class);
startActivity(intent);
default:
break;
}
}
}
[/java]
完整项目下载地址:
http://pan.baidu.com/s/1gdJuRXx