我使用首选xml文件作为选项菜单,我在我的java类中使用它,如bellwo 但编译器说它已弃用,并在addpreffrencefromresource(R.xml.mypreff)上添加了一条黑线。什么是编码的新方法?谢谢你的帮助。
package com.bestdiet;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.MenuItem;
public class prefs extends PreferenceActivity{
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.mypref);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
// return super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.exit:
finish();
break;
case R.id.help:
break;
case R.id.options:
break;
}
return false;
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:title="پخش صدا"
android:defaultValue="true"
android:key="checkbox1"
/>
<CheckBoxPreference
android:title="پخش موسیقی"
android:defaultValue="true"
android:key="checkbox2"/>
</PreferenceScreen>
答案 0 :(得分:1)
有一些值得注意的事情:我只使用了相同的xml首选项布局...
但是没有人告诉你不要使用两个不同的文件(比如prefs_old.xml和prefs_new.xml),将一些新功能添加到PreferenceScreen的(新)PreferenceFragment版本中。
这是我的PreferenceActivity。它只是检查构建版本以查看必须准备哪个版本的首选项:
package com.example.android.scheduler2;
/* ---------------------------------- Imports ------------------------------- */
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.ListView;
public final class ACT_Prefs
extends PreferenceActivity
implements OnSharedPreferenceChangeListener
{
/* ------------------------------ Objects ------------------------------- */
private Context ctx = null;
/* ----------------------------- Overrides ------------------------------ */
@Override
public final void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ctx = getApplicationContext();
if (Build.VERSION.SDK_INT < 11)
{
createPreference_Activity();
}
else
{
createPreference_Fragment();
}
}
@Override
protected void onPause()
{
// Unregister OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
unregisterOnSharedPreferenceChangeListener(this);
// Call base method
super.onPause();
}
@Override
protected void onResume()
{
// Register OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
registerOnSharedPreferenceChangeListener(this);
// Fire base method
super.onResume();
}
@Override
public void onSharedPreferenceChanged
(final SharedPreferences sharedPreferences, final String key)
{
// ... Do whatever you need to do here ...
System.out.println(key + " changed!!");
}
/* ------------------------------ Methods ------------------------------- */
//@SuppressWarnings("deprecation")
private final void createPreference_Activity()
{
addPreferencesFromResource(R.xml.prefs);
}
@SuppressLint("NewApi")
private final void createPreference_Fragment()
{
getFragmentManager().beginTransaction().replace
(android.R.id.content, new FRG_Prefs()).commit();
getFragmentManager().executePendingTransactions();
}
}
这是FRG_Prefs(PreferenceFragment)
package com.example.android.scheduler2;
/* ---------------------------------- Imports ------------------------------- */
import android.annotation.SuppressLint;
import android.graphics.PixelFormat;
import android.preference.PreferenceFragment;
import android.view.View;
import android.widget.ListView;
@SuppressLint("NewApi")
public final class FRG_Prefs
extends PreferenceFragment
{
/* ----------------------------- Overrides ------------------------------ */
@Override
public final void onResume()
{
super.onResume();
addPreferencesFromResource(R.xml.prefs);
}
}