应用程序与复选框应该做的相反

时间:2014-09-29 01:12:05

标签: java android checkbox

我正在制作应用程序,我正在遇到路障。 基本上我有一个复选框...如果选中该复选框,则应该启用编辑文本首选项中的文本输入,并使用该值。但问题出在哪里......如果未选中该框,则使用EditTextPreference中的值。如果选中此框,则使用硬编码值。

我的部分活动:

private void loadURI()
  {
      PreferenceManager.setDefaultValues(this, R.xml.settings, true);
      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
      if (settings.getBoolean("default_uri", false)){
            String defaultURI = getString(R.string.dream_uri);
            this.mWebView.loadUrl(defaultURI);
      }
      else {
          String customURI = settings.getString("custom_uri", "");
          this.mWebView.loadUrl(customURI);
      }

  }

将Settings.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="Backdrop Settings" >
        <CheckBoxPreference
            android:title="Use Custom Backdrop URL"
            android:id="@id/checkBox1"
            android:key="default_uri"
            android:defaultValue="true"/>
        <EditTextPreference
            android:title="Custom Backdrop URL"
            android:summary="Tap here to enter custom URL"
            android:id="@id/editText1"
            android:defaultValue="Enter Custom URL here"
            android:key="custom_uri"
            android:dependency="default_uri" />
        <CheckBoxPreference
            android:title="Use a Custom Slideshow Period?"
            android:key="slide_duration"
            android:defaultValue="false"/>
        <ListPreference
            android:title="Slideshow Duration"
            android:summary="How long should each slide be displayed?"
            android:key="list_duration"
            android:dependency="slide_duration"/>
    </PreferenceCategory>
    <PreferenceCategory
        android:title="Other">
        <Preference
            android:title="Test Dream"
            android:summary="Tap here to test the dream"
            android:key="testDream" />
        <Preference
            android:title="About"
            android:key="AboutApp"
            android:summary="About this app" />
    </PreferenceCategory>
</PreferenceScreen>

我的偏好设施活动:

public class mollySettings extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        Preference button = findPreference("testDream");
        button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference arg0) {
                final Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setClassName("com.android.systemui", "com.android.systemui.Somnambulator");
                startActivity(intent);
                return true;
            }
        });

        Preference about = findPreference("AboutApp");
        about.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference arg1) {
                final Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setClassName("com.death2all110.flogging.molly", "com.death2all110.flogging.molly.About");
                startActivity(intent);
                return true;
            }
        });


    }
}

提前致谢,如果您还有其他需要,请告诉我

1 个答案:

答案 0 :(得分:0)

弄清楚了。

我在if语句中添加了一个惊叹号。所以loadURI方法现在是:

private void loadURI()
  {
      PreferenceManager.setDefaultValues(this, R.xml.settings, true);
      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
      if (!settings.getBoolean("default_uri", false)){
            String defaultURI = getString(R.string.dream_uri);
            this.mWebView.loadUrl(defaultURI);
      }
      else {
          String customURI = settings.getString("custom_uri", "");
          this.mWebView.loadUrl(customURI);
      }

  }

而不是:

private void loadURI()
  {
      PreferenceManager.setDefaultValues(this, R.xml.settings, true);
      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
      if (settings.getBoolean("default_uri", false)){
            String defaultURI = getString(R.string.dream_uri);
            this.mWebView.loadUrl(defaultURI);
      }
      else {
          String customURI = settings.getString("custom_uri", "");
          this.mWebView.loadUrl(customURI);
      }

  }