今天完成了newapp项目。
实现了:
1.向另一个activity传递user和pass,并显示;
2.清空文本框的内容;
3.实现了实时更新电子钟;
4.显示当前日期;
5.弹出式日期选择器(DatePickerDialog)。
解决方法:(http://blog.youkuaiyun.com/Android_Tutor/article/details/5740845)
1. 首先建立了一个userInfo.java保存用户信息的类,将用户输入的信息保存在userInfo的一个对象中,使用Bundle对象mbundle的putSerializable()方法与intent.putExtras(mbundle)实现。方法为:
userinfo muserInfo=new userinfo();
muserInfo.setName(user.getText().toString());
muserInfo.setPassWord(pass.getText().toString());
Intent intent=new Intent();
intent.setClass(MainActivity.this, second.class);
Bundle mbundle=new Bundle();
mbundle.putSerializable(USRINFO, muserInfo);
intent.putExtras(mbundle);
startActivity(intent);
finish();
方法二:使用PacelableMethod()
{
public void PacelableMethod(){
Book mBook = new Book();
mBook.setBookName("Android Tutor");
mBook.setAuthor("Frankie");
mBook.setPublishTime(2010);
Intent mIntent = new Intent(this,ObjectTranDemo2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, mBook);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
}
2.在清空按钮的click事件中对文本框(tet)进行如下操作:tet.setText("");
3.电子时钟:
onCreate(){
mTime=(TextView)findViewById(R.id.time);
new TimeThread().start();
}
public class TimeThread extends Thread {
@Override
public void run () {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
}
catch (InterruptedException e) {
e.printStackTrace();
}
} while(true);
}
}
private Handler mHandler = new Handler() {
public void handleMessage (Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case msgKey1:
long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format("hh:mm:ss", sysTime);
mTime.setText(sysTimeStr);
break;
default:
break;
}
}
};
4.显示当前时间 private void dateInit() {
final Calendar c=Calendar.getInstance();
mYear=c.get(Calendar.YEAR);
mMonth=c.get(Calendar.MONTH);
mDay=c.get(Calendar.DAY_OF_MONTH);
int week=c.get(Calendar.DAY_OF_WEEK);
Map<Integer, String> map=new HashMap<Integer, String>();
map.put(1, "星期天");map.put(2, "星期一");map.put(3, "星期二");map.put(4, "星期三");map.put(5, "星期四");map.put(6, "星期五");map.put(7, "星期六");
date.setText("今天是:"+mYear+"年"+(mMonth+1)+"月"+mDay+map.get(week));
}
5.日期选择器
changeDate=(Button)findViewById(R.id.change);
//changeDate.setOnClickListener(CDListener);
changeDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
protected void updateDisplay() {
date.setText(new StringBuilder().append("时间:").append(mYear).append("-").append(mMonth+1).append("-").append(mDay));
}
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
}
需要注意代码规范:
1.监听器全部为this接受
sButton.setOnClickListener(this);
pButton.setOnClickListener(this);
2.接收按钮响应
public void onClick(View v) {
if(v == sButton){
SerializeMethod(); //单击时响应的方法
}else{
PacelableMethod();
}
}
实现了:
1.向另一个activity传递user和pass,并显示;
2.清空文本框的内容;
3.实现了实时更新电子钟;
4.显示当前日期;
5.弹出式日期选择器(DatePickerDialog)。
解决方法:(http://blog.youkuaiyun.com/Android_Tutor/article/details/5740845)
1. 首先建立了一个userInfo.java保存用户信息的类,将用户输入的信息保存在userInfo的一个对象中,使用Bundle对象mbundle的putSerializable()方法与intent.putExtras(mbundle)实现。方法为:
userinfo muserInfo=new userinfo();
muserInfo.setName(user.getText().toString());
muserInfo.setPassWord(pass.getText().toString());
Intent intent=new Intent();
intent.setClass(MainActivity.this, second.class);
Bundle mbundle=new Bundle();
mbundle.putSerializable(USRINFO, muserInfo);
intent.putExtras(mbundle);
startActivity(intent);
finish();
方法二:使用PacelableMethod()
{
public void PacelableMethod(){
Book mBook = new Book();
mBook.setBookName("Android Tutor");
mBook.setAuthor("Frankie");
mBook.setPublishTime(2010);
Intent mIntent = new Intent(this,ObjectTranDemo2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, mBook);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
}
2.在清空按钮的click事件中对文本框(tet)进行如下操作:tet.setText("");
3.电子时钟:
onCreate(){
mTime=(TextView)findViewById(R.id.time);
new TimeThread().start();
}
public class TimeThread extends Thread {
@Override
public void run () {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
}
catch (InterruptedException e) {
e.printStackTrace();
}
} while(true);
}
}
private Handler mHandler = new Handler() {
public void handleMessage (Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case msgKey1:
long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format("hh:mm:ss", sysTime);
mTime.setText(sysTimeStr);
break;
default:
break;
}
}
};
4.显示当前时间 private void dateInit() {
final Calendar c=Calendar.getInstance();
mYear=c.get(Calendar.YEAR);
mMonth=c.get(Calendar.MONTH);
mDay=c.get(Calendar.DAY_OF_MONTH);
int week=c.get(Calendar.DAY_OF_WEEK);
Map<Integer, String> map=new HashMap<Integer, String>();
map.put(1, "星期天");map.put(2, "星期一");map.put(3, "星期二");map.put(4, "星期三");map.put(5, "星期四");map.put(6, "星期五");map.put(7, "星期六");
date.setText("今天是:"+mYear+"年"+(mMonth+1)+"月"+mDay+map.get(week));
}
5.日期选择器
changeDate=(Button)findViewById(R.id.change);
//changeDate.setOnClickListener(CDListener);
changeDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
protected void updateDisplay() {
date.setText(new StringBuilder().append("时间:").append(mYear).append("-").append(mMonth+1).append("-").append(mDay));
}
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
}
需要注意代码规范:
1.监听器全部为this接受
sButton.setOnClickListener(this);
pButton.setOnClickListener(this);
2.接收按钮响应
public void onClick(View v) {
if(v == sButton){
SerializeMethod(); //单击时响应的方法
}else{
PacelableMethod();
}
}