无法通过屏幕显示设置选项

时间:2015-05-15 21:20:02

标签: java android android-studio sharedpreferences preferences

编辑 在底部添加MyActivity.java(即主要活动) EDIT2 MyActivity.java添加了一行(这解决了问题)

我已设置首选项但无法访问它们。无论我在xml中选择style,无论我在Android Studio(AS)1.1.0中选择哪种虚拟设备或样式,屏幕都缺少下面显示的3个点。甚至包含LightActionBarDarkActionBar的下拉样式也不会显示点。

enter image description here

在xml中,我已经尝试了<style name="AppBaseTheme" parent="android:Holo.ButtonBar">,它最终在一个小应用程序上工作了(同样的问题),并且对于parent,我尝试了{{1}和其他事情。

如果看到3个点,我就不在乎了。只需要公开首选项屏幕。

我还为Base.Theme.AppCompat.Light.DarkActionBar尝试了neverifroomalways

showAsAction

此处<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MyActivity"> <item android:id="@+id/itemFocus" android:title="@string/focusAtClue" android:orderInCategory="200" app:showAsAction="never"/>

preferences.xml

此处<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory > <CheckBoxPreference android:key="@string/focusAfterShow" android:title="@string/focusAfterShow" android:summary="Always place the cursor at the 'clue' (sum) after tapping 'Show'." android:defaultValue="true" /> </PreferenceCategory> <PreferenceCategory > <CheckBoxPreference android:key="@string/screenSaver" android:title="@string/screenSaver" android:summary="Keep screen on at all times while running this app." android:defaultValue="true" /> </PreferenceCategory> </PreferenceScreen>

SettingsFragment.java

import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.util.Log; public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } @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()); if (key.equalsIgnoreCase("pie_type")){ Log.w("Settings", sharedPref.getString(key, "")); } } }

SettingsActivity.java

此处 import android.app.Activity; import android.os.Bundle; public class SettingsActivity extends Activity { public static final String SETTINGS = "com.whatever.kakurocombosbuildvariants.settings"; public static final String FIRST_USE = "com.whateverkakurocombosbuildvariants.firstUse"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); } } 中调用了SettingsActivity

MyActivity.java

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_settings: Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } (主要活动; 300条遗留代码删除)

MyActivity.java

/////////////////////// EDIT2 /////////////////////// /////

public class MyActivity extends Activity {

  public final String
      prefix = "com.XXXX.kakurocombosbuildvariants"
      , SETTINGS =        prefix + ".settings"
      , FIRST_USE =       prefix + ".firstUse"
      , FOCUS_AT_CLUE =   prefix + ".focusAtClue"
      , SCREENSAVER =     prefix + ".screensaver"
      , literally_Focus_At_Clue = "Focus at clue"
      , literally_Screen_saver  = "Screen saver"
      ;
  public boolean firstUse;

  SharedPreferences preferences;
  SharedPreferences.Editor editor;

  boolean screenSaver;//= false;
  boolean focusAtClue ;//= true;

  AlertDialog alertDialog;

  private void makeActionOverflowMenuShown() {
    //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
    try {
      ViewConfiguration config = ViewConfiguration.get(this);
      Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
      if (menuKeyField != null) {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(config, false);
      }
    } catch (Exception e) {
      popupMessage("Problem making actionbar overflow");
    }
  }

  void showKeypad(){
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  }

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

  @Override public boolean onPrepareOptionsMenu(Menu menu)
  {
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId()) {
      case R.id.menu_settings:
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);

        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void setScreensaver()
  {
    if( ! screenSaver) getWindow().addFlags  (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    else               getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

  @Override protected void
  onCreate(Bundle savedInstanceState) // ************************** ON CREATE **********
  {
    super.onCreate(savedInstanceState);

/////////////////////////// EDIT2 ///////////////////////////////////////    

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getWindow().setFormat(Window.FEATURE_ACTION_BAR);

/////////////////////////// EDIT2 ///////////////////////////////////////    

    if(! FREE) setContentView(R.layout.activity_my);
    else       setContentView(R.layout.activity_free);

    SharedPreferences preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);

    firstUse = preferences.getBoolean(FIRST_USE, true);
    if(firstUse){
      Toast.makeText(getApplicationContext(), "Welcome to Kakuro Combos", Toast.LENGTH_SHORT).show();
      editor = preferences.edit();
      editor.putBoolean(FIRST_USE, false);
      editor.commit();
    }

    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK",
                          new DialogInterface.OnClickListener() { public void
                                                                  onClick(DialogInterface dialog, int which)
                          {
                            dialog.dismiss();
                          }});
    showKeypad();

    makeActionOverflowMenuShown();

    getWindow().setFormat(Window.FEATURE_ACTION_BAR);

    showKeypad();

    setScreensaver();


  } // onCreate
}

/////////////////////// EDIT2 /////////////////////// /////

2 个答案:

答案 0 :(得分:1)

看起来主要问题是您没有给菜单xml充气。

尝试对ActionBarActivity使用MainActivity,然后添加onCreateOptionsMenu()以扩充菜单xml。

public class MyActivity extends ActionBarActivity{

    //...........

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    //............
}

答案 1 :(得分:1)

您需要加载菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.<your_menu>, menu);
    //...
}