我正在尝试制作以下项目:开始活动有 4 个按钮。1. 第一个按钮只打开一个信息对话框 (onClick0) 2. 第二个按钮打开一个 DatePickerDialog 并允许我设置日期。(onClick1) 3. 第三个按钮打开一个对话框,显示我输入的日期 (onClick2)。4.第四个按钮只是打开一个关于对话框。(onClick3)
问题是在第一个日期设置之后,我打开的对话框按第三个按钮总是显示我设置的第一个日期。
我究竟做错了什么?请帮我 :)
主要活动代码:
package boy.girl;
import java.text.DateFormat;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;
import android.content.DialogInterface;
import android.app.DatePickerDialog;
import android.app.AlertDialog;
public class MainActivity extends Activity {
DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
Calendar dateAndTime=Calendar.getInstance();
int mYear,mMonth,mDay;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick0(View v)
{
showDialog(0);
}
public void onClick1(View v)
{
new DatePickerDialog(this, d,
dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH))
.show();
}
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(getBaseContext(), "Set", Toast.LENGTH_SHORT).show();
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
dateAndTime.set(Calendar.YEAR, year);
dateAndTime.set(Calendar.MONTH, monthOfYear);
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
Toast.makeText(getBaseContext(),
Integer.toString(mDay)+" "+Integer.toString(mMonth)+" "+Integer.toString(mYear),
Toast.LENGTH_SHORT).show();
}
};
public void onClick2(View v)
{
showDialog(2);
}
public void onClick3(View v) { showDialog(3); }
@Override
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case 0:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.infobox);
builder.setTitle("Title Information");
builder.setMessage("Information text...");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
.show();
}
});
return builder.create();
case 2:
return new AlertDialog.Builder(this)
.setTitle("Results")
.setMessage(Integer.toString(mDay)+" "+Integer.toString(mMonth)+
" "+Integer.toString(mYear))
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
.show();
}
}
).create();
case 3:
return new AlertDialog.Builder(this)
.setTitle("About")
.setMessage("About this company and App version")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
.show();
}
}
).create();
}
return null;
}
}