2016-07-14 4 views
1

내 주요 활동에서 학생이 공유 환경 설정에 자신의 ID를 저장했는지 확인합니다. onResume에 문자열이 비어 있는지 확인하고, 그렇다면 대화 상자를 엽니 다.활동에서 시작 PreferenceDialog

private void showNoStudentIdDialog() { 
    LayoutInflater inflater = LayoutInflater.from(this); 
    View view = inflater.inflate(R.layout.dialog_no_id, null); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    builder.setView(view).setTitle(getString(R.string.no_id_dialog_title)); 
    builder.setCancelable(false); 
    builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 

      openSettingsDetail(); 
     } 
    }); 
    builder.create().show(); 
} 

openSettingDetail

public boolean openSettingsDetail() { 
    Intent settingsIntent = new Intent(this, SettingsActivity.class); 
    startActivity(settingsIntent); 
    return true; 
} 

의도가 제대로 내 SettingsActivty로드 올바른 환경 설정을 시작합니다. 그러나 ID 설정을 수동으로 터치하여 대화 상자를 시작해야합니다. 인 텐트를 시작할 때 MainActivity에서 프로그래밍 방식으로이 터치/클릭을 얻을 수 있습니까? 다음 launche이 대화는 처음 표시

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res/com.mcgowan.timetable.itsligotimetables"> 


<com.mcgowan.timetable.itsligotimetables.StudentIdEditTextPreference 
    android:capitalize="words" 
    android:defaultValue="@string/student_id_default" 
    android:hint="@string/student_id_hint" 
    android:inputType="textCapWords" 
    android:key="@string/student_id_key" 
    android:maxLength="9" 
    custom:minLength="9" 
    android:maxLines="1" 
    android:selectAllOnFocus="true" 
    android:singleLine="true" 
    android:title="@string/pref_title_display_name" /> 


<ListPreference 
    android:defaultValue="@string/pref_timetable_type_classes" 
    android:entries="@array/timetable_type_list_titles" 
    android:entryValues="@array/timetable_type_list_values" 
    android:key="@string/pref_timetable_key" 
    android:title="@string/pref_title_timetable_type" /> 

SettingsActivity

public class SettingsActivity extends AppCompatPreferenceActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setupActionBar(); 
    addPreferencesFromResource(R.xml.pref_general); 

    bindPreferenceSummaryToValue(findPreference(getString(R.string.student_id_key))); 
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_timetable_key))); 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == android.R.id.home) { 
     this.finish(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* Set up the {@link android.app.ActionBar}, if the API is available. 
*/ 
private void setupActionBar() { 
    ActionBar actionBar = getSupportActionBar(); 
    if (actionBar != null) { 
     // Show the Up button in the action bar. 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setIcon(R.drawable.settings); 
    } 
} 


/** 
* {@inheritDoc} 
*/ 
@Override 
public boolean onIsMultiPane() { 
    return isXLargeTablet(this); 
} 

/** 
* Helper method to determine if the device has an extra-large screen. For 
* example, 10" tablets are extra-large. 
*/ 
private static boolean isXLargeTablet(Context context) { 
    return (context.getResources().getConfiguration().screenLayout 
      & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; 
} 


/** 
* A preference value change listener that updates the preference's summary 
* to reflect its new value. 
*/ 
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { 
    @Override 
    public boolean onPreferenceChange(Preference preference, Object value) { 
     String stringValue = value.toString(); 

     if (preference.getKey().equals(preference.getContext().getString(R.string.student_id_key))) { 
      setPreferenceSummary(preference, stringValue); 
     } else if (preference instanceof ListPreference) { 
      // For list preferences, look up the correct display value in 
      // the preference's 'entries' list. 
      ListPreference listPreference = (ListPreference) preference; 
      int index = listPreference.findIndexOfValue(stringValue); 

      // Set the summary to reflect the new value. 
      preference.setSummary(
        index >= 0 
          ? listPreference.getEntries()[index] 
          : null); 

     } else if (preference instanceof RingtonePreference) { 
      // For ringtone preferences, look up the correct display value 
      // using RingtoneManager. 
      if (TextUtils.isEmpty(stringValue)) { 
       // Empty values correspond to 'silent' (no ringtone). 
       preference.setSummary(R.string.pref_ringtone_silent); 

      } else { 
       Ringtone ringtone = RingtoneManager.getRingtone(
         preference.getContext(), Uri.parse(stringValue)); 

       if (ringtone == null) { 
        // Clear the summary if there was a lookup error. 
        preference.setSummary(null); 
       } else { 
        // Set the summary to reflect the new ringtone display 
        // name. 
        String name = ringtone.getTitle(preference.getContext()); 
        preference.setSummary(name); 
       } 
      } 

     } else { 
      // For all other preferences, set the summary to the value's 
      // simple string representation. 
      preference.setSummary(stringValue); 
     } 
     return true; 
    } 
}; 

private static void setPreferenceSummary(Preference preference, Object value) { 
    String stringValue = value.toString(); 
    String key = preference.getKey(); 

    if (preference instanceof ListPreference) { 
     // For list preferences, look up the correct display value in 
     // the preference's 'entries' list (since they have separate labels/values). 
     ListPreference listPreference = (ListPreference) preference; 
     int prefIndex = listPreference.findIndexOfValue(stringValue); 
     if (prefIndex >= 0) { 
      preference.setSummary(listPreference.getEntries()[prefIndex]); 
     } 
    } else if (key.equals(preference.getContext().getString(R.string.student_id_key))) { 
     @TimetableSyncAdapter.ServerStatus int status = Utility.getServerStatus(preference.getContext()); 
     switch (status) { 
      case TimetableSyncAdapter.SERVER_STATUS_OK: 
       preference.setSummary(stringValue); 
       break; 
      case TimetableSyncAdapter.SERVER_STATUS_UNKNOWN: 
       preference.setSummary(preference.getContext().getString(R.string.pref_student_id_unknown_description, value.toString())); 
       break; 
      case TimetableSyncAdapter.SERVER_STATUS_ID_INVALID: 
       preference.setSummary(preference.getContext().getString(R.string.pref_student_id_invalid_id, value.toString())); 
       break; 
      default: 
       // Note --- if the server is down we still assume the value 
       // is valid 
       preference.setSummary(stringValue); 
     } 
    } else { 
     // For other preferences, set the summary to the value's simple string representation. 
     preference.setSummary(stringValue); 
    } 

} 

/** 
* Binds a preference's summary to its value. More specifically, when the 
* preference's value is changed, its summary (line of text below the 
* preference title) is updated to reflect the value. The summary is also 
* immediately updated upon calling this method. The exact display format is 
* dependent on the type of preference. 
* 
* @see #sBindPreferenceSummaryToValueListener 
*/ 
private static void bindPreferenceSummaryToValue(Preference preference) { 
    // Set the listener to watch for value changes. 
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); 

    // Trigger the listener immediately with the preference's 
    // current value. 
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, 
      PreferenceManager 
        .getDefaultSharedPreferences(preference.getContext()) 
        .getString(preference.getKey(), "")); 
} 

/** 
* This method stops fragment injection in malicious applications. 
* Make sure to deny any unknown fragments here. 
*/ 
protected boolean isValidFragment(String fragmentName) { 
    return PreferenceFragment.class.getName().equals(fragmentName) 
      || GeneralPreferenceFragment.class.getName().equals(fragmentName) 
      || DataSyncPreferenceFragment.class.getName().equals(fragmentName) 
      || NotificationPreferenceFragment.class.getName().equals(fragmentName); 
} 

/** 
* This fragment shows general preferences only. It is used when the 
* activity is showing a two-pane settings UI. 
*/ 
@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public static class GeneralPreferenceFragment extends PreferenceFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.pref_general); 
     setHasOptionsMenu(true); 



     // Bind the summaries of EditText/List/Dialog/Ringtone preferences 
     // to their values. When their values change, their summaries are 
     // updated to reflect the new value, per the Android Design 
     // guidelines. 

     bindPreferenceSummaryToValue(findPreference(getString(R.string.student_id_key))); 
     bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_timetable_key))); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == android.R.id.home) { 
      getActivity().finish(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 


/** 
* This fragment shows notification preferences only. It is used when the 
* activity is showing a two-pane settings UI. 
*/ 
@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public static class NotificationPreferenceFragment extends PreferenceFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.pref_notification); 
     setHasOptionsMenu(true); 

     // Bind the summaries of EditText/List/Dialog/Ringtone preferences 
     // to their values. When their values change, their summaries are 
     // updated to reflect the new value, per the Android Design 
     // guidelines. 
     bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone")); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == android.R.id.home) { 
      getActivity().finish(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

/** 
* This fragment shows data and sync preferences only. It is used when the 
* activity is showing a two-pane settings UI. 
*/ 
@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public static class DataSyncPreferenceFragment extends PreferenceFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.pref_data_sync); 
     setHasOptionsMenu(true); 

     // Bind the summaries of EditText/List/Dialog/Ringtone preferences 
     // to their values. When their values change, their summaries are 
     // updated to reflect the new value, per the Android Design 
     // guidelines. 
     bindPreferenceSummaryToValue(findPreference("sync_frequency")); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == android.R.id.home) { 
      getActivity().finish(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

}

pref_general.xml, 의도

enter image description here

목적은 다음과 같은 화면 발사 S :

enter image description here

나는 시작하는 학생 ID 기본 설정을 클릭해야 다음

enter image description here

두 번째 이미지에서 단계를 건너 뛰고 싶습니다. 그리고 programaticall 마지막으로 의도 한 것을 시작하십시오.

답변

0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setupActionBar(); 
    addPreferencesFromResource(R.xml.pref_general); 

    bindPreferenceSummaryToValue(findPreference(getString(R.string.student_id_key))); 
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_timetable_key))); 

    if (<student ID is empty)) { 
    showNoStudentIdDialog(); 
    } 

} 

예, 알고 있습니다 만, 나는 당신이 묻는 것을 정말로 혼란스럽게 생각합니다. 대화 상자를 표시하려면 메서드를 호출하여 원하는 조건과 관계없이 표시하십시오.

+0

자세한 내용으로 내 질문을 업데이트했습니다. –

+0

대화 상자를 표시하려면 표시하십시오. 내 답변에 코드를 넣는 것처럼. 나는 네가 그 일을 못하게하는 것을 이해하지 못하고있다. –

+0

이것은 사용자가 설정 활동을로드하기 전에 앱을 처음 실행할 때 확인한 것입니다. 내 질문에 다른 이미지를 추가했습니다 –