所以我已經達到了我的繩索,當談到這個黨 datePicker對話框,我正在與。我已經通過我的應用程序中的7 不同的活動,並且在我到達datePicker之前沒有遇到過單個 問題。我的問題包含兩個 元素:1)我的datePicker的dang標題顯示星期三, 0002年12月召喚。 datePicker本身正確顯示 ,但不是標題,並且我沒有改變標題 位。當您更改日期時,標題將更改爲正確的 信息,但......除外。2)看起來好像星期幾 已關閉(表示2011年2月22日星期一)。所以我 要發佈我的整個代碼在這個活動,如果任何人有 任何想法任何幫助,非常感謝。這個問題將出現在onCreateDialog或onPrepareDialog方法的 中。如果我需要刪除 任何其他無關的初始化方法,我會這樣做,但我會 喜歡驗證我不會在 此活動中的其他任何地方造成問題。datePicker問題,標題初始化很時髦
public class QuizSettingsActivity extends QuizActivity {
SharedPreferences mGameSettings;
static final int DATE_DIALOG_ID = 0;
static final int PASSWORD_DIALOG_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//Retrieve the shared preferences
mGameSettings = getSharedPreferences(GAME_PREFERENCES,
Context.MODE_PRIVATE);
//Initialize nickname entry
initNicknameEntry();
//Initialize email entry
initEmailEntry();
//Initialize the password chooser
initPasswordChooser();
//Initialize date picker
initDatePicker();
//Initialize spinner
initGenderSpinner();
//Initialize clear
initClear();
}//end of on Create
//Initialize clear
private void initClear() {
Button clear = (Button)findViewById(R.id.BTN_Clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText nickname =
(EditText)findViewById(R.id.ET_Nickname);
nickname.setText("");
EditText email =
(EditText)findViewById(R.id.ET_Email);
email.setText("");
TextView dob =
(TextView)findViewById(R.id.TV_DOB_Info);
dob.setText(R.string.settings_dob_not_set);
Editor editor = mGameSettings.edit();
editor.remove(GAME_PREFERENCES_DOB);
editor.remove(GAME_PREFERENCES_GENDER);
//editor.clear();
editor.commit();
}//End of onClick
});//end of on click listener
}//end of clear method
//Handles logging upon leaving settings screen
@Override
protected void onDestroy(){
Log.d(DEBUG_TAG, "SHARED PREFERENCES");
Log.d(DEBUG_TAG, "Nickname is: "
+ mGameSettings.getString(GAME_PREFERENCES_NICKNAME,
"Not Set"));
Log.d(DEBUG_TAG, "Email is: "
+ mGameSettings.getString(GAME_PREFERENCES_EMAIL, "Not
Set"));
Log.d(DEBUG_TAG, "Gender (U=0, M=1, F=2) is: " +
+ mGameSettings.getInt(GAME_PREFERENCES_GENDER, 0));
//Don't save password as of yet
Log.d(DEBUG_TAG, "Password is: "
+ mGameSettings.getString(GAME_PREFERENCES_PASSWORD,
"Not Set"));
//Don't save dob yet
Log.d(DEBUG_TAG, "DOB is: "
+ DateFormat.format("MMMM, dd, yyyy",
mGameSettings.getLong(GAME_PREFERENCES_DOB,
0)));
super.onDestroy();
}//end of on Destroy
//Initialize spinner
private void initGenderSpinner() {
//Configuring spinner controls...load spinner
final Spinner spinner = (Spinner)
findViewById(R.id.Spinner_Gender);
ArrayAdapter<?> adapter =
ArrayAdapter.createFromResource(this,
R.array.gender, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if(mGameSettings.contains(GAME_PREFERENCES_GENDER)){
spinner.setSelection(mGameSettings.getInt(GAME_PREFERENCES_GENDER,
0));
}
//Handle spinner selection
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View
itemSelected,
int selectedItemPosition, long selectedId){
Editor editor = mGameSettings.edit();
editor.putInt(GAME_PREFERENCES_GENDER,
selectedItemPosition);
editor.commit();
}
public void onNothingSelected(AdapterView<?> parent){
}
});
}//end of spinner
//Initialize Date Picker
private void initDatePicker() {
//Set Date Info
TextView dobInfo = (TextView) findViewById(R.id.TV_DOB_Info);
if(mGameSettings.contains(GAME_PREFERENCES_DOB)){
dobInfo.setText(DateFormat.format("MMMM dd, yyyy",
mGameSettings.getLong(GAME_PREFERENCES_DOB, 0)));
}else {
dobInfo.setText(R.string.settings_dob_not_set);
}
//Handle date picking dialog
Button pickDate = (Button) findViewById(R.id.Button_DOB);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
/*Toast.makeText(QuizSettingsActivity.this,
"TODO: Launch DatePicker Dialog",
Toast.LENGTH_LONG).show();*/
}//end of onClick
});//end of OnClick Listener
}//end of date picker
@Override
protected Dialog onCreateDialog(int id){
switch(id){
case DATE_DIALOG_ID:
final TextView dob =
(TextView)findViewById(R.id.TV_DOB_Info);
DatePickerDialog dateDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view,
int year, int monthOfYear, int dayOfMonth)
{
Time dateOfBirth = new Time();
dateOfBirth.set(dayOfMonth, monthOfYear,
year);
long dtDob = dateOfBirth.toMillis(true);
dob.setText(DateFormat.format("MMMM dd,
yyyy", dtDob));
Editor editor = mGameSettings.edit();
editor.putLong(GAME_PREFERENCES_DOB,
dtDob);
editor.commit();
}//end of onDateSet
},0,0,0);
return dateDialog;
case PASSWORD_DIALOG_ID:
//build dialog inflate/load
LayoutInflater inflater =
(LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
final View layout =
inflater.inflate(R.layout.password_dialog,
(ViewGroup)findViewById(R.id.root));
final EditText p1 =
(EditText)layout.findViewById(R.id.EditText_Pwd1);
final EditText p2 =
(EditText)layout.findViewById(R.id.EditText_Pwd2);
final TextView error = (TextView)
layout.findViewById(R.id.TextView_PwdProblem);
p2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {//adding
import adds beforeTextChanged
//&
onTextChanged methods
String strPass1 = p1.getText().toString();
String strPass2 = p2.getText().toString();
if (strPass1.equals(strPass2)) {
error.setText(R.string.settings_pwd_equal);
} else {
error.setText(R.string.settings_pwd_not_equal);
}
}
@Override//added with import of afterTextChanged
public void beforeTextChanged(CharSequence s, int
start,
int count, int after) {
// TODO Auto-generated method stub
}
@Override//added with import of afterTextChanged
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
});//end of text changed listener
AlertDialog.Builder builder = new
AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle(R.string.settings_button_pwd);
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
whichButton) {
// dismiss and remove the Dialog, so it
// cannot be used again (no cached info)
QuizSettingsActivity.this.removeDialog(PASSWORD_DIALOG_ID);
}//end of onClick
});//end of on click listener
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
TextView passwordInfo =
(TextView)
findViewById(R.id.TV_Password_Info);
String strPassword1 = p1.getText().toString();
String strPassword2 = p2.getText().toString();
if (strPassword1.equals(strPassword2)) {
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_PASSWORD,
strPassword1);
editor.commit();
passwordInfo.setText(R.string.settings_pwd_set);
} else {
Log.d(DEBUG_TAG, "Passwords do not match. Not
saving. Keeping old password (if set).");
}
//dismiss and remove the dialog so it
// cannot be used again using removeDialog
QuizSettingsActivity.this.removeDialog(PASSWORD_DIALOG_ID);
}//end of onClick
});//end of onClick listener
//create and call dialog and return it
AlertDialog passwordDialog = builder.create();
return passwordDialog;
}//end of switch
return null;
}//end of Dialog on CreateDialog method
@Override
protected void onPrepareDialog(int id, Dialog dialog){
super.onPrepareDialog(id, dialog);
switch(id){
case DATE_DIALOG_ID:
//Handle any date picker dialog initialization here
DatePickerDialog dateDialog = (DatePickerDialog) dialog;
int iDay, iMonth, iYear;
//check for date of birth preference
if(mGameSettings.contains(GAME_PREFERENCES_DOB)){
//retrieve dob setting from preferences
long msBirthDate =
mGameSettings.getLong(GAME_PREFERENCES_DOB, 0);
Time dateOfBirth = new Time();
dateOfBirth.set(msBirthDate);
iDay = dateOfBirth.monthDay;
iMonth = dateOfBirth.month;
iYear = dateOfBirth.year;
} else {
final Calendar cal = Calendar.getInstance();
//Today's date fields
iDay = cal.get(Calendar.DAY_OF_MONTH);
iMonth = cal.get(Calendar.MONTH);
iYear = cal.get(Calendar.YEAR);
//dateDialog.setTitle("Birthday");
//dateDialog.updateDate(2000,11,25);
}
//set the date in the date picker to the date of birth
//OR to the current date
dateDialog.updateDate(iYear, iMonth, iDay);
return;
case PASSWORD_DIALOG_ID:
return;
}//end of switch
}//end of onPrepareDialog method
//Initialize Password Chooser
private void initPasswordChooser() {
// Set password info
TextView passwordInfo = (TextView)
findViewById(R.id.TV_Password_Info);
if(mGameSettings.contains(GAME_PREFERENCES_PASSWORD)){
passwordInfo.setText(R.string.set_password);
}else{
passwordInfo.setText(R.string.settings_pwd_not_set);
}
//Handle password setting dialog
Button setPassword = (Button)
findViewById(R.id.Button_Password);
setPassword.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(PASSWORD_DIALOG_ID);
/*Toast.makeText(QuizSettingsActivity.this, "TODO:
Launch Password Dialog",
Toast.LENGTH_LONG).show();*/
}
});
}//End of Password Chooser
//Initialize Email entry
private void initEmailEntry() {
// Save Email
final EditText emailText = (EditText)
findViewById(R.id.ET_Email);
if (mGameSettings.contains(GAME_PREFERENCES_EMAIL)){
emailText.setText(mGameSettings.getString(GAME_PREFERENCES_EMAIL,
""));
}
emailText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((event.getAction() == KeyEvent.ACTION_DOWN ||
event.getAction() == KeyEvent.ACTION_UP) &&
(keyCode == KeyEvent.KEYCODE_ENTER || keyCode ==
KeyEvent.KEYCODE_TAB)){
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_EMAIL,
emailText.getText().toString());
editor.commit();
return true;
}
return false;
}
});
}//end of email
//Initialize Nickname entry
private void initNicknameEntry() {
// Save Nickname
final EditText nicknameText = (EditText)
findViewById(R.id.ET_Nickname);
if (mGameSettings.contains(GAME_PREFERENCES_NICKNAME)){
nicknameText.setText(mGameSettings.getString(GAME_PREFERENCES_NICKNAME,
""));
}
nicknameText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((event.getAction() == KeyEvent.ACTION_DOWN ||
event.getAction() == KeyEvent.ACTION_UP) &&
(keyCode == KeyEvent.KEYCODE_ENTER || keyCode ==
KeyEvent.KEYCODE_TAB)){
String strNickname =
nicknameText.getText().toString();
Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_NICKNAME,
strNickname);
editor.commit();
return true;
}
return false;
}
});
}//End of nickname
}//end of QuizSettingsActivity
我也嘗試setTitle,但只有在日期改變之前才能運行。
非常感謝......我相信這不會是我在樹林的這個脖子上的最後一篇文章。我會重新研究一些,看看它會把我帶到哪裏。雖然我還沒有這樣做,你知道這是否會解決我正在經歷的時髦標題方面? – FROSTYSMOOTH
我對你的問題的第一部分有點困惑。什麼不正確顯示在問題的#1部分中? –
當datePicker初始化關閉按鈕時,它會顯示:0002年12月31日星期三作爲標題直到日期改變,然後標題顯示正確的信息。 – FROSTYSMOOTH