Android Studio(AS)1.1.0(和Eclipse Luna)中不推荐使用的方法“设置活动”

时间:2015-05-14 01:36:27

标签: java android eclipse android-fragments

我的应用程序可能有几个复选框,我宁愿用户每次从内存中追逐应用程序时都不必重新检查。所以我一直在阅读偏好。

我不确定我是否看到过像“基于片段的现代PreferenceActivity”那样复杂的东西,所以我一直在寻找AS 1.1.0的指导/示例,但我得到的代码总共有9个引用4个代码到addPreferencesFromResourcegetPreferenceScreenfindPreference,所有这些代码都带有这种模糊:

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

让两个IDE使用不推荐的代码呈现代码是一回事;没有任何迹象表明每种方法被替换为另一种方法(但没有任何东西取代它,因为它发生了)。

AS真的难以制作“基于片段的现代PreferenceActivity”吗?如果是这样,我想我的复杂性是正确的。

我错过了什么吗?我应该只编写/扩展已弃用的代码吗?关闭编译器检查已弃用的东西?

有没有人知道一个很好的教程(或一个例子)一个简单的(如两个复选框)“现代基于片段的PreferenceActivity”?或者是什么可以保存偏好但不被弃用的东西?

1 个答案:

答案 0 :(得分:0)

我问,"有没有人知道一个很好的教程(或一个例子)一个简单的(如两个复选框)modern fragment-based PreferenceActivity?或者任何可以保存偏好但不被弃用的东西?"

我昨晚9点做了。 * 2015年5月16日编辑*

这是一个完整的,有效的工作偏好活动,但它没有所有可绘制的,mipmap,dimension和values文件夹和文件。我从Sams 自学 Android应用程序开发24小时的建议中下载并修改了它。强调我的。

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, ""));
       }
    }
}

RES \菜单\ 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>

RES \布局\ 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>

RES \布局\ 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>

的strings.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:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

RES \ 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>