安卓获取首选项值



我正在制作一个应用程序。有一个带有设置选项的动作栏。其中有一个EditTextPpreferences。如何获取用户在那里编写的文本?我尝试了很多代码,但不起作用。有人可以帮助我吗?

这是我的主要活动:

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

}

这是设置活动:

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)

你可以尝试做(在创建你的片段

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;
}    

相关内容

  • 没有找到相关文章

最新更新