简体   繁体   English

如何在.java代码中启用禁用的(在xml中)ListPreference对象?

[英]How do I enable a disabled (in xml) ListPreference object in .java code?

EDIT 3 编辑3

Here's how I determine if the device is a tablet in onCreate for main Activity : 这是我在onCreate确定设备是否为平板电脑的主要Activity

  public static boolean isTablet(Context ctx){
    return (ctx.getResources().getConfiguration().screenLayout
        & Configuration.SCREENLAYOUT_SIZE_MASK
    )
        >= Configuration.SCREENLAYOUT_SIZE_LARGE;
  }

I don't know if that's a hack and not reliable, but in any event, I don't know how to use it in onCreate in the Fragment (below) because I don't know what Context to pass to isATablet . 我不知道这是否是一种破解并且不可靠,但是无论如何,我都不知道如何在Fragment onCreate中使用它(如下),因为我不知道将什么Context传递给isATablet

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);Preference p = getPreferenceManager().findPreference("orientation");
    p.setEnabled(isTablet(????????????????));

EDIT showing arrays as defined in strings.xml as requested: 编辑显示按要求在strings.xml定义的数组:

<string-array name="modes">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>
<string-array name="indices">
    <item>Portrait</item>
    <item>Landscape</item>
    <item>Rotate</item>
</string-array>

* END EDIT * *结束编辑*

How do I enable this ListPreference object in .java code? 如何在.java代码中enableListPreference对象?

<PreferenceCategory>
    <ListPreference
        android:key="orientation"
        android:title="Orientation (for tablets only)"
        android:enabled="false"
        android:summary="Lock in portrait or landscape mode or neither (allow rotation to determine)?"
        android:defaultValue="0"
        android:entries="@array/indices"
        android:entryValues="@array/modes"
        >
    </ListPreference>
</PreferenceCategory>

I can't make Android Studio 1.1.0 let me give it an id ; 我不能让Android Studio 1.1.0让我给它一个id ; but if there's a way, there's my solution--how do I do it? 但是如果有办法,那就是我的解决方案-我该怎么办?

EDIT 编辑

Workaround: 解决方法:

android:enabled="true"

if(isATablet) { // ***************************
  switch (preferences.getString(ORIENTATION, "0")) {
    case "0":
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      break;
    case "1":
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      break;
    case "2":
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
      break;
    default:
      Toast.makeText(getApplicationContext(), "Bad orientation", Toast.LENGTH_SHORT).show();
  }
}
else{ // ***************************
  editor = preferences.edit(); // ***************************
  editor.putString(ORIENTATION,"0"); // ***************************
  editor.commit(); // ***************************
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

Very lame. 很la脚 Please help! 请帮忙!

It looks like you can just use the PreferenceManager and get the Preference based on the key . 看来您可以只使用PreferenceManager并基于key获得Preference。 See documentation here and here . 在此处此处查看文档。

You could use the method described here to determine if it's a tablet or not. 您可以使用此处描述的方法来确定它是否是平板电脑。

Then, it's just as simple as getting the preference, and enabling it if it's a tablet! 然后,就像获得首选项一样简单,如果是平板电脑则启用它!

Edit: 编辑:

In order to use your method and give it a valid Context , you could do it like this. 为了使用您的方法并为其提供有效的Context ,您可以这样做。 Note that I used a modified version of the code posted here for the isTablet() method: 请注意,我对isTablet()方法使用了此处发布的代码的修改版本:

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

            Preference p = getPreferenceManager().findPreference("orientation");
            p.setEnabled(isTablet(getActivity()));
        }

        private boolean isTablet(Context context) {
            int screenLayout = context.getResources().getConfiguration().screenLayout;
            screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;

            switch (screenLayout) {
                case Configuration.SCREENLAYOUT_SIZE_SMALL:
                    return false;
                case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                    return false;
                case Configuration.SCREENLAYOUT_SIZE_LARGE:
                    return true;
                case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9
                    return true;
                default:
                    return false;
            }
        }
        //...................

Thanks to incredible generosity of Daniel Nugent, here's what finally worked. 感谢Daniel Nugent的慷慨慷慨,这才是最终成功的方法。

preferences.xml (don't enable or disable orientation): preferences.xml (不启用或禁用定向):

    <PreferenceCategory>
        <ListPreference
            android:key="orientation"
            android:title="Orientation (for tablets only)"
            android:summary="Lock in portrait or landscape mode or neither (allow rotation to determine)?"
            android:defaultValue="0"
            android:entries="@array/indices"
            android:entryValues="@array/modes"
            >
        </ListPreference>
    </PreferenceCategory>
</PreferenceScreen>

SettingsFragment.java : SettingsFragment.java

public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
//  next two lines ****************************************************
    Preference p = getPreferenceManager().findPreference("orientation");
    p.setEnabled(isTablet(getActivity()));
//  Let java code enable or disable orientation ***********************
  }

  @Override
  public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  }
  @Override
  public void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  }
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());

  }
}

isTablet.java : isTablet.java

  private boolean isTablet(Context context) {
    int screenLayout = context.getResources().getConfiguration().screenLayout;
    screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;

    switch (screenLayout) {
      case Configuration.SCREENLAYOUT_SIZE_SMALL:
        return false;
      case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        return false;
      case Configuration.SCREENLAYOUT_SIZE_LARGE:
        return true;
      case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9
        return true;
      default:
        return false;
    }
  }

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

相关问题 如何获取XML中定义的ListPreference的默认值? - How can I get the default value of a ListPreference as defined in the XML? 如何在 Java 中为 Selenium webdriver 启用禁用的按钮? - How to enable a disabled button in Java for Selenium webdriver? 如何在Java中启用禁用的JSP按钮? - How to enable a disabled JSP button in Java? 如何使用内联架构处理XML字符串到java对象? - How do I Process an XML string with inline schema to a java object? 如何将XML转换为java值对象? - How do I convert XML into a java value object? 如何将xml中的相应标签放置到Java对象 - How do I place the corresponding tags in this xml to a java object 如何使用Java编辑文件对象中的XML节点 - How do I edit a XML node in a file object, using Java 我该如何通过代码进行Java对象分配跟踪 - how can I do java object allocation tracing from with code 如何使用 java 代码而不是 xml 代码将按钮添加到 android 工作室中的另一个活动 - How do I add button to another activity in android studio using java code rather than xml code 如何启用通知并能够读取我的 BLE 代码? - How do I enable notifications and to be able read in my BLE code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM