简体   繁体   English

无法显示设置选项的屏幕

[英]Can't get screen to show settings options

EDIT Added MyActivity.java (ie, main activity) at bottom EDIT2 Added lines to MyActivity.java (this solved the problem) 编辑在底部添加了MyActivity.java (即主要活动) EDIT2MyActivity.java添加了行(这解决了问题)

I have preferences set up but have no way to access them. 我已经设置了首选项,但是无法访问它们。 No matter what style I pick in xml and no matter what virtual device or style I pick in Android Studio (AS) 1.1.0, the screen lacks the 3 dots shown below. 无论我在xml中选择哪种style ,无论在Android Studio(AS)1.1.0中选择哪种虚拟设备或样式,屏幕都缺少以下三个点。 Not even the pulldown styles that include LightActionBar and DarkActionBar show the dots. 甚至包括LightActionBarDarkActionBar的下拉样式DarkActionBar显示点。

在此处输入图片说明

In xml, I've tried <style name="AppBaseTheme" parent="android:Holo.ButtonBar"> , which finally worked last night (was having same problem) on a small app, and also, for parent , I tried Base.Theme.AppCompat.Light.DarkActionBar and other things. 在xml中,我尝试了<style name="AppBaseTheme" parent="android:Holo.ButtonBar"> ,该方法昨晚终于在一个小型应用程序上工作了(有同样的问题),并且对于parent ,我尝试了Base.Theme.AppCompat.Light.DarkActionBar等。

I don't so much care if I see the 3 dots; 我不太在乎是否看到三个点。 just ANYTHING to expose the preferences screen. 只需显示任何东西即可显示首选项屏幕。

I've also tried never , ifroom , and always for showAsAction : 我也尝试never ifroomifroom ,并且always尝试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"/>

Here's preferences.xml : 这是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>

Here's SettingsFragment.java : 这是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, ""));
    }
  }
}

And SettingsActivity.java : 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);
  }

}

Here's where SettingsActivity is invoked in MyActivity.java : 这是在MyActivity.java调用SettingsActivity的位置:

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);
    }
  }

MyActivity.java (main activity; 300 LINES OF EXTRANEOUS CODE DELETED) MyActivity.java (主要活动;删除了300行多余的代码)

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 //////////////////////////// //////////////////////// EDIT2 //////////////////////////// //

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

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

It looks like main issue is that you're not inflating your menu xml. 看来主要问题是您没有膨胀菜单XML。

Try using ActionBarActivity for your MainActivity , and add onCreateOptionsMenu() in order to inflate the menu xml. 尝试对MainActivity使用ActionBarActivity ,并添加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;
    }

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

You need to load the menu: 您需要加载菜单:

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

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

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