gpt4 book ai didi

java - 无法让屏幕显示设置选项

转载 作者:行者123 更新时间:2023-11-29 20:46:28 24 4
gpt4 key购买 nike

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

我设置了首选项,但无法访问它们。不管怎样style我在 xml 中选择,无论我在 Android Studio (AS) 1.1.0 中选择什么虚拟设备或样式,屏幕都缺少如下所示的 3 个点。甚至包括 LightActionBar 的下拉样式也不行和 DarkActionBar显示点。

enter image description here

在 xml 中,我试过 <style name="AppBaseTheme" parent="android:Holo.ButtonBar"> ,昨晚终于在一个小应用程序上工作了(遇到了同样的问题),而且,parent , 我试过 Base.Theme.AppCompat.Light.DarkActionBar和其他东西。

如果我看到 3 个点,我不会太在意;只需公开首选项屏幕即可。

我也试过 never , ifroom , 和 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"/>

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

}

这里是SettingsActivityMyActivity.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);
}
}

MyActivity.java (主要 Activity ;删除了 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
}

//////////////////////编辑2///////////////////////////

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

//////////////////////编辑2///////////////////////////

最佳答案

看起来主要问题是您没有扩充菜单 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;
}

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

关于java - 无法让屏幕显示设置选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30268909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com