2015-05-14 31 views
0

我的應用程序可能有一些複選框,我寧願用戶不必每次重新檢查應用程序從內存中追趕。所以我一直在閱讀有關偏好。Android Studio(AS)1.1.0(和Eclipse Luna)中的棄用方法「設置活動」

我不確定我看到任何複雜的「基於現代片段的PreferenceActivity」,所以我一直在尋找來自AS 1.1.0的指導/示例,但我得到的代碼總共有9個引用所有這些都在4個大塊的代碼addPreferencesFromResourcegetPreferenceScreen,和findPreference,攜帶這種書籍說明:

This method was deprecated in API level 11. This function is not relevant for a modern fragment-based PreferenceActivity.

要由兩個IDE中已經提出了用棄用代碼的代碼是一回事沒有任何跡象表明每種方法已被替換爲另一種方法(但沒有任何替代方法,因爲它發生)。

對於AS來說,生成「基於片段的現代PreferenceActivity」真的很難嗎?如果是這樣,我想我是對的複雜性。

我錯過了什麼嗎?我應該只寫/擴展棄用的代碼嗎?關閉不推薦使用的東西的編譯器檢查?

有沒有人知道一個簡單的(或兩個複選框)「現代片段爲基礎的PreferenceActivity」(或一個例子)的一個很好的教程?或任何保存偏好但不被棄用的東西?

回答

0

我問:「有人知道一個簡單的(或兩個複選框)modern fragment-based PreferenceActivity?或任何保存偏好但不被棄用的好教程嗎?」

我昨晚晚上9點才做。 * EDITED 5/16/15 *

這是一個完整的工作首選項活動,儘管它沒有所有可繪製,mipmap,維度和值文件夾和文件。我從Sams的建議中下載了它自學 24小時Android應用開發並對其進行了修改。強調我的。

enter image description here

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.whatever.prefs" 
      android:versionCode="1" 
      android:versionName="1.0" > 
    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.whatever.prefs.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.whatever.prefs.SettingsActivity" 
      android:label="@string/title_activity_settings" > 
     </activity> 
    </application> 
</manifest> 

MainActivity.java:

package com.whatever.prefs; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    public static final String SETTINGS = "com.whatever.prefs.settings"; 
    public static final String FIRST_USE = "com.whatever.prefs.firstUse"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    /* BAD IDEA--CREATES NEW SET OF PREFS 
    SharedPreferences preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE); 
    Do this v v v v v v v v v v v instead! */ 
    preferences = PreferenceManager.getDefaultSharedPreferences(this); 

    boolean firstUse = preferences.getBoolean(FIRST_USE, true); 
    if (firstUse){ 
     Toast helloMessage = Toast.makeText(getApplicationContext(), "Hello First Time 
            User",Toast.LENGTH_LONG); 
     helloMessage.show(); 
     Editor editor = preferences.edit(); 
     editor.putBoolean(FIRST_USE, false); 
     editor.commit(); 
    } 
    else { 
     Toast.makeText(getApplicationContext(), "Second+ use", Toast.LENGTH_SHORT).show(); 
    } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_settings: 
     Intent intent = new Intent(this, SettingsActivity.class); 
     startActivity(intent); 

     return true; 

     default: 
     return super.onOptionsItemSelected(item); 
    } 
    } 
} 

SettingsFragment.java:

package com.whatever.prefs; 

import android.content.SharedPreferences; 
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 
import android.os.Bundle; 
import android.preference.PreferenceFragment; 
import android.preference.PreferenceManager; 
import android.util.Log; 

public class SettingsFragment extends PreferenceFragment 
            implements OnSharedPreferenceChangeListener { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
    } 
    @Override 
    public void onResume() { 
     super.onResume(); 
     getPreferenceScreen().getSharedPreferences(). 
            registerOnSharedPreferenceChangeListener(this); 
    } 
    @Override 
    public void onPause() { 
     super.onPause(); 
     getPreferenceScreen().getSharedPreferences(). 
            unregisterOnSharedPreferenceChangeListener(this); 
    } 
    @Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {  
     SharedPreferences sharedPref = 
            PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     if (key.equalsIgnoreCase("pie_type")){ 
      Log.w("Settings", sharedPref.getString(key, "")); 
     } 
    } 
} 

重小號\菜單\ activity_main.xml中:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context=".MainActivity"> 
    <item 
     android:id="@+id/menu_settings" 
     android:orderInCategory="100" 
     app:showAsAction="ifRoom" 
     android:title="@string/menu_settings"/> 
</menu> 

資源\佈局\ activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context=".MainActivity" > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Hi world" /> 
</RelativeLayout> 

資源\佈局\ activity_settings.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent">  
    <fragment android:name="com.whatever.prefs.SettingsFragment" 
       android:id="@+id/settings_fragment" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
</RelativeLayout> 

字符串。XML:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Hour11App</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title_activity_settings">Settings</string> 
    <string-array name="pie_array"> 
     <item>apple</item> 
     <item>blueberry</item> 
     <item>cherry</item> 
     <item>coconut cream</item> 
    </string-array> 
</resources> 

styles.xml:

<resources> 
    <!-- Base application theme, dependent on API level. This theme is replaced 
     by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> 
    <style name="AppBaseTheme" parent="android:Holo.ButtonBar"> 
    <!-- Theme customizations available in newer API levels can go in 
      res/values-vXX/styles.xml, while customizations related to 
      backward-compatibility can go here.--> 
    </style> 
    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    </style> 
</resources> 

SettingsActivity.java:

package com.whatever.prefs; 

import android.app.Activity; 
import android.os.Bundle; 

public class SettingsActivity extends Activity { 
    public static final String SETTINGS = "com.whatever.prefs.settings"; 
    public static final String FIRST_USE = "com.whatever.prefs.firstUse"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 
    } 
} 

dimens.xml:

資源\ XML \的preferences.xml:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
    <PreferenceCategory 
     android:title="Images"> 
     <CheckBoxPreference 
      android:key="hires" 
      android:title="Hi-Res Images" 
      android:summary="Show high quality images. These take longer to load" 
      android:defaultValue="False" /> 
    </PreferenceCategory> 
    <PreferenceCategory 
     android:title="Pie Info"> 
     <CheckBoxPreference 
      android:key="pie" 
      android:title="Pie" 
      android:summary="Like Pie" 
      android:defaultValue="true" /> 
     <ListPreference 
      android:dependency="pie" 
      android:key="pie_type" 
      android:title="Pie Type" 
      android:summary="Preferred pie type for eating" 
      android:dialogTitle="Type of Pie" 
      android:entries="@array/pie_array" 
      android:entryValues="@array/pie_array" 
      android:defaultValue="apple" /> 
     <EditTextPreference 
      android:key="more_info" 
      android:title="More Info" 
      android:summary="More about pies" 
      android:defaultValue="" /> 
    </PreferenceCategory> 
    <PreferenceScreen 
      android:key="second_preferencescreen" 
      android:title="Second Screen of Settings"> 
     <EditTextPreference 
      android:key="extraA" 
      android:title="More Data" 
      android:summary="Another EditTextPreference" 
      android:defaultValue="" /> 
     <EditTextPreference 
      android:key="ExtraB" 
      android:title="Even More Info" 
      android:summary="What more can we say" 
      android:defaultValue="" /> 
    </PreferenceScreen> 
</PreferenceScreen> 
相關問題