Android 获取偏好值

Android get the preferences values

我正在制作一个应用程序。有一个带有设置选项的 ActionBar。 其中有一个 EditTextPreference。 我怎样才能得到用户在那里写的文本? 我尝试了很多代码,但没有用。 有人可以帮我吗?

这是我的 MainActivity:

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, LocationListener {

public static FragmentManager fragmentManager;
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
boolean intervall=false;

boolean started;

static String intervalloption = "INTERVALLOPTION";
Intent intervalloptionintent = new Intent(intervalloption);
Bundle intervalloptionbundle = new Bundle();

Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewpager = (ViewPager) findViewById(R.id.pager);
    ft = new FragmentPageAdapter(getSupportFragmentManager());

    fragmentManager = getSupportFragmentManager();

    actionbar = getActionBar();
    viewpager.setAdapter(ft);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar
            .addTab(actionbar.newTab().setText("Run").setTabListener(this));
    actionbar
            .addTab(actionbar.newTab().setText("Statistics").setTabListener(this));

    viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();




    if (id == R.id.share) {
        Toast.makeText(getApplicationContext(), "share", Toast.LENGTH_LONG).show();
        return true;
    }
    if (id == R.id.action_settings) {
        Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
        startActivity(intent);
        Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();


        return true;
    }
    if (id == R.id.exit) {
        Toast.makeText(getApplicationContext(), "exit", Toast.LENGTH_LONG).show();
        finish();
        System.exit(0);

        return true;
    }

    return super.onOptionsItemSelected(item);
}


}

这是 SettingsActivity:

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{

static String intervalloption = "INTERVALLOPTION";
Intent intervalloptionintent = new Intent(intervalloption);
Bundle intervalloptionbundle = new Bundle();



@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //addPreferencesFromResource(R.layout.preferences);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    settings.registerOnSharedPreferenceChangeListener(this);
    Toast.makeText(getApplicationContext(), "Preference", Toast.LENGTH_LONG).show();
}



public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.preferences);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key == "walk") {
            String walktime = sharedPreferences.getString(key, "walk");
            Toast.makeText(getActivity(), String.valueOf(walktime), Toast.LENGTH_LONG).show();
            // do stuff
        }


    }
}



public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    if (key == "walk") {
        String walktime = sharedPreferences.getString(key, "walk");
        Toast.makeText(getApplicationContext(), String.valueOf(walktime), Toast.LENGTH_LONG).show();
        // do stuff
    }


}


}

也许只是

key.equals("walk")

而不是

key == "walk"

您不应该将字符串与 == 进行比较,而应将其与 .equals()

进行比较

此外,我认为您的 preferences 文件应该在 res/xml 而不是 res/layout,因此您可以调用 addPreferencesFromResource(R.xml.preferences)

您可以尝试做(在您的片段的 onCreate 中)

EditTextPreference pref = (EditTextPreference)findPreference(key of your EditTextPreference);
pref.setOnPreferenceChangeListener(this)

并实现接口然后

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String walktime = newValue.toString();
 Toast.makeText(getActivity(), String.valueOf(walktime), Toast.LENGTH_LONG).show();
    return true;
}