如何从preferences的sumary [英] How to get the sumary from preferences

查看:453
本文介绍了如何从preferences的sumary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题:
我有一个的EditText preference让在应用程序缺省路径的用户类型。

One question: I have a EditTextPreference to let the user type in a default path for the app.

我如何管理新的值是preferencefragment我用待观察?

How can I manage that the new value is to be seen in the preferencefragment I use?

用户在preference窗口中单击OK完成新的设置,我想写的新路径作为标题下面的摘要后。

After the user clicked ok in the preference window to complete the new settings I want to write the new path as a summary below the title.

我曾尝试已与onShared preferenceChanged经历,但没有奏效。我不知道如何从弹出窗口进入编辑字段中的用户文本是

I have tried already experiencing with the onSharedPreferenceChanged but it did not work. I do not know how to get access to the edit field from the popup window where the users text is in.

希望能找到一些帮助
安德烈亚斯!

Hope to find some help Andreas!

编辑:
这里是源$ C ​​$ C为我所用,并与鲁斯塔姆的建议修改了整个preferenceFragment。遗憾的是它不工作。

Here is the source code for the whole PreferenceFragment I used and modified with the suggestions of Rustam. Unfortunately it does not work.

package com.example.wbsettings;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;

public class PreferenceFrag extends PreferenceFragment 
implements SharedPreferences.OnSharedPreferenceChangeListener{

public static final String KEYVAL = "startpath"; 
SharedPreferences sp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    EditTextPreference prefUrl = (EditTextPreference) findPreference(KEYVAL);
    prefUrl.getEditText().setHint("default path");

    //>> Here the app crashed when I debug it
         ------------------------------------ 
    prefUrl.setSummary(sp.getString(KEYVAL, ""));

}//onCreate

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
    Preference pref = findPreference(key);
    if (pref instanceof EditTextPreference) {
        EditTextPreference etp = (EditTextPreference) pref;
        pref.setSummary(etp.getText());

    }
}//onSharedPreferenceChanged

}

我也不在你的榜样明白什么:在哪里是preferences与OnShared preferenceChangeListener注册?我的意思是,截至目前我永远达不到,因为飞机坠毁的onShared preferenceChanged过程。但是,当我使语句的注释和应用程序正在运行,并完成了我从来没有停在我设定的onShared preferenceChanged过程中的断点。

What I also do not understand in your example: Where is the preferences to register with the OnSharedPreferenceChangeListener? I mean as of now I never reach the onSharedPreferenceChanged procedure because of the crash. But when I make the statement a comment and the app is running and finished I have never been stopped at the breakpoint I set within the onSharedPreferenceChanged procedure.

另一个问题:
什么是这里回答一个评论,如果答案就再评论过长最好的做法?我试图打开一个答案,我自己的问题。但是,一个弹出告诉我,这不是事情应该是这样。那么怎么办?

Another question: What is the best practice here to answer a comment if the answer becomes too long for another comment? I tried to open an answer to my own question. But a pop up informed me that this is not the way it should be. So what to do?

问候安德烈亚斯!

推荐答案

menu_setting.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"

        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/menu_settings"
        android:icon="@android:drawable/ic_menu_preferences"/>

</menu>

preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <EditTextPreference
        android:dialogTitle="Enter path"
        android:key="prefPath"
        android:summary=""
        android:title="Path Setting" />

</PreferenceScreen>

preferenceFrag 应该是这样的:

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;

public class PreferenceFrag extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener{

public static final String KEYVAL = "prefPath"; 
SharedPreferences sp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preference);

    EditTextPreference prefPath = (EditTextPreference) findPreference(KEYVAL);
    prefPath.getEditText().setHint("sdcard/path");

    // Here the app crashed when I debug it

     SharedPreferences sp = getPreferenceScreen().getSharedPreferences();

         prefPath.setSummary(sp.getString(KEYVAL, ""));



}//onCreate


@Override
public void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
    super.onPause();
    // Unregister the listener whenever a key changes
    getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}


public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
    Preference pref = findPreference(key);
    if (pref instanceof EditTextPreference) {
        EditTextPreference etp = (EditTextPreference) pref;
        pref.setSummary(etp.getText());

    }
}//onSharedPreferenceChanged

}

preferenceActivity

import android.app.Activity;
import android.os.Bundle;

public class PreferenceActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);

  getFragmentManager().beginTransaction().replace(android.R.id.content,
                new PreferenceFrag()).commit();
 }

}

MainActivity

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview=(TextView)findViewById(R.id.textview);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_setting, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.menu_settings:
            Intent i = new Intent(this, PreferenceActivity.class);
            startActivity(i);
            break;

        }

        return true;
    }



}

最后的Andr​​oidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.preferencetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.preferencetest.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.example.preferencetest.PreferenceActivity"/>
    </application>

</manifest>

这篇关于如何从preferences的sumary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆