Posted on 2009-11-04 17:17
疯狂 阅读(7290)
评论(0) 编辑 收藏 所属分类:
android
代码:
package com.tishi;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
public class TiShi extends Activity implements OnClickListener,
OnDateSetListener {
/** *//** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button click1 = (Button) findViewById(R.id.click1);
final Button click2 = (Button) findViewById(R.id.click2);
final Button click3 = (Button) findViewById(R.id.click3);
click1.setOnClickListener(this);
click2.setOnClickListener(this);
click3.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.click1:
showToast();
break;
case R.id.click2:
showAlertDialog();
break;
case R.id.click3:
showDatePickerDialog();
break;
default:
break;
}
}
public void showToast() {
Toast.makeText(TiShi.this, "这是一个提示", Toast.LENGTH_SHORT).show();
}
public void showAlertDialog() {
new AlertDialog.Builder(TiShi.this).setTitle("标题")
.setMessage("提示内容").setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
setResult(RESULT_OK);// 确定按钮事件
finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView txt = (TextView) findViewById(R.id.text);
txt.setText("你选择了取消。");
}
}).show();
}
public void showDatePickerDialog() {
Calendar d = Calendar.getInstance(Locale.CHINA);
d.setTime(new Date());
new DatePickerDialog(this, this, d.get(Calendar.MONTH), d
.get(Calendar.DAY_OF_MONTH), d.get(Calendar.DAY_OF_YEAR))
.show();
}
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
TextView txt = (TextView) findViewById(R.id.text);
txt.setText(Integer.toString(year) + "-"
+ Integer.toString(monthOfYear) + "-"
+ Integer.toString(dayOfMonth));
}
}