2016-07-14 16 views
1

In meiner Haupttätigkeit, überprüfen ich, ob ein Student ihre ID in gemeinsamen Einstellungen gespeichert In onResume, überprüfe ich, ob die Zeichenfolge leer ist, und wenn ja, ich starte ein DialogfeldStarten PreferenceDialog von Aktivität

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; 
} 

Die Absicht richtig startet meine SettingsActivty und lädt die richtigen Einstellungen. Ich muss jedoch immer noch die ID-Einstellung manuell berühren, um den Dialog dafür zu starten. Kann ich diesen Touch/Klick programmatisch von MainActivity aus erreichen, wenn ich die Absicht starte?

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

<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" /> 

Dieser Dialog zuerst angezeigt wird, die dann launche s eine Absicht

enter image description here

Die Absicht startet die folgende Anzeige:

enter image description here

und ich habe auf der Teilnehmer-ID Vorzug klicken Sie auf die folgende

enter image description here

zu starten

Ich möchte den Schritt im zweiten Bild überspringen und programmatisch y starten Sie die letzte mit der Absicht

Antwort

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(); 
    } 

} 

Ja, ich weiß, aber ich bin wirklich verwirrt, was Sie fragen. Wenn Sie den Dialog anzeigen möchten, rufen Sie einfach die Methode auf, um sie überall und unter jeder gewünschten Bedingung anzuzeigen ...

+0

Ich habe meine Frage mit weiteren Details aktualisiert –

+0

Wenn Sie den Dialog anzeigen möchten, zeigen Sie ihn einfach an. Wie ich den Code in meine Antwort eingefügt habe. Ich verstehe nicht, was dich daran hindert. –

+0

Dies ist eine Überprüfung, die beim ersten Start der App durchgeführt wird, bevor der Benutzer die Einstellungsaktivität lädt. Ich habe ein anderes Bild in meiner Frage –