次のプロジェクトを作成しようとしています: Start Activity には 4 つのボタンがあります。1. 最初のボタンは情報ダイアログ (onClick0) を開きます。 2. 2 番目のボタンは DatePickerDialog を開き、日付を設定できるようにします。(onClick1) 3. 3 番目のボタンは、入力した日付を示すダイアログを開きます (onClick2)。4. 4 番目のボタンをクリックすると、[バージョン情報] ダイアログが開きます。(onClick3)
問題は、最初の日付が設定された後、3番目のボタンを押して開いたダイアログに、最初に設定した最初の日付が常に表示されることです。
私は何を間違っていますか?私を助けてください :)
MainActivity コード:
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;
}
}