我正在编写一个 Android 应用程序,用户必须在其中选择在图表上显示的方式和内容。这些选项在两个单选菜单组(单选按钮)中表示,它们都应该可以从操作栏访问。
第一组工作正常。它像这样添加到我的 ActionBar XML 的末尾:
<group android:checkableBehavior="single" android:showAsAction="never" >
<item android:id="@+id/menu_choice_1" android:title="Choice 1" />
<item android:id="@+id/menu_choice_2" android:title="Choice 2" android:checked="true"/>
</group>
当我添加第二个 <group>然而,在第一个下方,两个合并到一个单选列表中。换句话说,两个列表中的选项一起呈现,如果我选择与第一个列表相关的选项,我不能从第二个列表中选择任何选项。
相反,我想要两个单独的单选按钮列表。我的下一个想法是在 ActionBar 添加另一个按钮,单击该按钮将启动 pop-up menu。 。但是当我点击按钮时,我得到一个
这是我尝试的弹出菜单代码: 在我的 ActionBar XML 中: 我的新菜单 XML: 我 Activity 中的代码: 如何创建两组可检查的菜单项,以便它们都附加到 ActionBar?IllegalStateException ,说我的“MenuPopupHelper 不能在没有 anchor 的情况下使用”。<item android:id="@+id/menu_openothermenu"
android:title="@string/openothermenustr"
android:showAsAction="always" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/menu_2_choice_1" android:title="@string/2_choice_1" />
<item android:id="@+id/menu_2_choice_2" android:title="@string/2_choice_2" android:checked="true"/>
</group>
</menu>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor;
switch (item.getItemId()) {
case R.id.openothermenu:
Menu m = (Menu) findViewById(R.menu.other_menu);
PopupMenu popup = new PopupMenu(this, findViewById(R.menu.main_menu));
popup.setOnMenuItemClickListener(this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.other_menu, popup.getMenu());
/* This commented block doesn't work either, and prevents execution
// Restore saved chosen value
int chosen = settings.getInt(MENU_2_PREFS, -1);
switch(chosen)
{
case 1:
m.findItem(R.id.menu_1_choice_1).setChecked(true);
updateVisibleThings();
break;
default:
case 2:
m.findItem(R.id.menu_2_choice_2).setChecked(true);
updateOtherVisibleThings();
break;
}
*/
popup.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor;
switch(item.getItemId()) {
case R.id.menu_2_choice_1:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
updateVisibleThings();
// save chosen setting
editor = settings.edit();
editor.putInt(MENU_2_PREFS, 1);
editor.commit(); // Commit the edits!
return true;
case R.id.menu_2_choice_2:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
updateOtherVisibleThings();
// save chosen setting
editor = settings.edit();
editor.putInt(MENU_2_PREFS, 2);
editor.commit(); // Commit the edits!
return true;
default:
return true;
}
}
最佳答案
我找到了一种优雅的方法来解决这个问题,不幸的是文档中没有。将以下代码添加到您的 ActionBar 菜单 XML:
<item android:id="@+id/menu_openothermenu"
android:title="@string/openothermenustr"
android:showAsAction="always">
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/menu_2_choice_1" android:title="@string/2_choice_1" android:showAsAction="never" />
<item android:id="@+id/menu_2_choice_2" android:title="@string/2_choice_2" android:showAsAction="never" android:checked="true" />
</group>
</menu>
</item>
出现这样的菜单不需要额外的处理程序代码或弹出菜单实现。
关于android - 在 ActionBar 中有两个单选组不起作用,但是附加一个弹出菜单也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12505362/
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案