简体   繁体   English

在android中主要活动中接收首选项更改

[英]Receiving preference changes in main activity in android

I'm having an issue with my android app. 我的Android应用程序有问题。 It is a simple application that is going to draw a rectangle of the specified size overtop of an image view in the main Activity. 这是一个简单的应用程序,它将在主Activity中绘制一个指定大小的矩形,超出图像视图的范围。 The issue is receiving the value of the changed preference that is the size of either the width or height of the rectangle. 问题是接收更改的首选项的值,该值是矩形的宽度或高度的大小。 I have tried multiple approaches. 我尝试了多种方法。 Right now I have my main activity, a preference fragment, and a ShowPreferences activity that is simply used by the main activity to create an intent and start that intent to show the preferences fragment. 现在我有我的主要活动,一个首选项片段和一个ShowPreferences活动,主活动只是使用它来创建一个意图并启动该意图以显示首选项片段。 This is done so that when I press the back button it returns the main screen without issue. 这样做是为了当我按下后退按钮时它返回主屏幕而没有问题。 I have tried to listen for the preference changes in the SettingsFragment but the issue is I can not access the variables in the main activity because calling getActivity() within the SettingsFragment returns ShowPreferences because that is technically the activity that called it through the intent. 我试图在SettingsFragment中侦听首选项更改,但问题是我无法访问main活动中的变量,因为在SettingsFragment中调用getActivity()会返回ShowPreferences,因为这在技术上是通过intent调用它的活动。 Thus I have moved on to detecting the preference changes through the main activity. 因此,我继续通过主要活动检测偏好变化。 Now the issue is that the findPreference() method cannot be resolved because it is within the main activity not the SettingsFragment class. 现在问题是findPreference()方法无法解析,因为它在主活动而不是SettingsFragment类中。 How can I turn the key into an EditTextPrefernce instance in order to make the necessary change to the variable in the main Activity. 如何将密钥转换为EditTextPrefernce实例,以便对主Activity中的变量进行必要的更改。 Any other suggestions would be greatly appreciated I am very new to android development. 非常感谢任何其他建议我是android开发的新手。 :) :)
Here is my code right now. 这是我现在的代码。

SettingsFragment (where the onSharedPreferenceChangeListener used to be) SettingsFragment(以前是onSharedPreferenceChangeListener)

public class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    }
}

ShowPreferences (simply to show the preference screen and allow for correct back button behavior) ShowPreferences(只是为了显示首选项屏幕并允许正确的后退按钮行为)

public class ShowPreferences extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preferences);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();

    }
}

Within the main activity the onSharedPreferencesChangedListener 在主活动中的onSharedPreferencesChangedListener

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    Preference pref = mContext.findPreference(key);
    if (pref instanceof EditTextPreference) {
        EditTextPreference etp = (EditTextPreference) pref;
        //now what?
        int newValue = Integer.parseInt(etp.getText());

        if(etp.toString().equals("Rectangle Width")){
            rectWidth = newValue;
        } else if (etp.toString().equals("Rectangle Height")){
            rectHeight = newValue;
        }
    }
}

Retrieve the value from SharedPreferences directly using the key. 使用密钥直接从SharedPreferences中检索值。

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key.equals("myKey"){
            int newValue = Integer.parseInt(sharedPreferences.getString(key, "0"));
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM