내 앱에는 앱이 메모리에서 쫓겨날 때마다 사용자가 다시 확인할 필요가없는 두 개의 확인란이있을 수 있습니다. 그래서 저는 선호도에 대해 읽었습니다.
"현대 조각 기반 PreferenceActivity"만큼 복잡한 것을 본 적이 있는지 잘 모르겠습니다. 따라서 AS 1.1.0에서 지침 / 예제를 찾고 있었지만 총 9 개의 참조가 포함 된 코드를 얻었습니다. 코드합니다 addPreferencesFromResource
, getPreferenceScreen
그리고 findPreference
, 모두가이 선전을 수행 :
This method was deprecated in API level 11. This function is not relevant for a modern fragment-based PreferenceActivity.
두 개의 IDE에서 더 이상 사용되지 않는 코드로 코드를 표시하는 것은 한 가지입니다. 각 방법이 다른 방법으로 대체되었는지에 대한 표시가 없다는 것을 알 수 있습니다 (하지만 대체 방법이 없습니다).
AS가 "현대적인 조각 기반 PreferenceActivity"를 생성하는 것이 정말 어려울까요? 그렇다면 복잡성에 대해 제가 옳다고 생각합니다.
내가 뭔가를 놓치고 있습니까? 더 이상 사용되지 않는 코드를 작성 / 확장해야합니까? 더 이상 사용되지 않는 항목에 대한 컴파일러 검사를 끄시겠습니까?
누구든지 간단한 (두 개의 확인란과 같은) "현대 조각 기반 PreferenceActivity"에 대한 멋진 자습서 (또는 예제)를 알고 있습니까? 또는 환경 설정을 저장하지만 더 이상 사용되지 않는 것이 있습니까?
나는 "간단한 (두 개의 체크 박스처럼)에 대한 멋진 튜토리얼 (또는 예제)을 아는 사람이 modern fragment-based PreferenceActivity
있습니까? 아니면 환경 설정을 저장하지만 더 이상 사용되지 않는 것이 있습니까?" 라고 물었습니다.
어젯밤 오후 9시를 기준으로합니다. * 15 년 5 월 16 일 수정 *
여기에 드로어 블, 밉맵, 치수 및 값 폴더와 파일이 모두 포함되어 있지는 않지만 완전한 작업 환경 설정 활동이 있습니다. Sams Teach Yourself Android App Development in 24 Hours의 조언에서 다운로드하여 수정했습니다. 내 강조.
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 \ menu \ 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 \ layout \ 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 \ layout \ 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>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다