我在动作栏中有一个菜单,我通过以下方式创建:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 98,Menu.NONE,R.string.filter).setIcon(R.drawable.ic_filter_list_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(Menu.NONE, 99,Menu.NONE,R.string.add).setIcon(R.drawable.ic_add_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Run Code Online (Sandbox Code Playgroud)
和menu_main.xml看起来像:
<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=".MainActivity">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:icon="@drawable/ic_settings_white_48dp"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
在Espresso中测试时,我想点击"添加"图标(menuId 99).我试过了
@Test
public void testAdd() {
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
onView(withText(R.string.add)).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
但是这会因NoMatchingViewException而失败.(设置项,直接在xml中定义,我可以使用相同的代码单击.)
这是针对targetSdkVersion 23和AppCompatActivity的.工具栏的相关行是:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
if( getSupportActionBar() != null ) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)
和tool_bar.xml看起来像:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/ColorPrimary"
android:elevation="4dp"
tools:ignore="UnusedAttribute">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud) 我有以下内容:
public class RandomList {
private List<Integer> list;
public List<Integer> getList() {
return list;
}
public RandomList (int n) {
list = new ArrayList<Integer>();
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
for (int i=0; i < n; i++)
{
Integer r = rand.nextInt();
list.add(r);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个填充随机整数值的列表.我想概括一下,也可以得到一个随机字符值列表或者其他类型的随机值列表.
所以我想要的是通用类型版本class RandomList<T>.我可以用"T"代替"Integer",但是我会卡在Integer r = rand.nextInt();不同类型的读取线上.
我正在考虑做以下事情:
instanceof检查传入的类对所需的类型(整数,字符...),并根据检查返回适当的随机值这有意义吗?有没有其他/更好的方法来实现我想要的?
是否可以使用 gettext 来翻译数据库表的内容?
例如,我有一些永远不会改变内容的数据库表,例如一个将国家 ID(“fr”、“de”、...)与国家名称(“法国”、“德国”...)连接起来的表,其中国名以英文书写。我可以添加额外的表列来提供各种语言的国名翻译,但我想知道是否可以使用 gettext 来翻译国名。
以防万一,我使用 php 并在代码中用_('text-for-translation').
我有一个EditText,我在那里听取文本的变化:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
// do stuff
}
});
Run Code Online (Sandbox Code Playgroud)
到目前为止这个工作正常,如果我在EditText中输入内容,就会执行afterTextChanged()中的内容.现在,在同一个活动中,我有一个ToggleButton,它可以改变EditText中的字符串.由于ToggleButton触发"afterTextChanged",如何防止此文本更改?
PS:不确定这是否相关,但具体来说我有一个接受小数或小数的EditText(例如"0.75"或"3/4"),切换按钮应在小数和小数显示之间切换,但不应触发任何内容在"afterTextChanged"中,因为值保持不变(3/4 = 0.75).
在我的模板引擎的配置中,我想添加SpringSecurityDialect():
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new SpringSecurityDialect());
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
return engine;
}
Run Code Online (Sandbox Code Playgroud)
但是蚀告诉我:
无法解析类型org.thymeleaf.dialect.IExpressionEnhancingDialect。从所需的.class文件间接引用它
这是什么意思,我该如何解决?
在pom.xml我有:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud) 我正在尝试测试某些 url 是否只能由具有某些权限的授权用户访问。因为我有自己的UserDetails,所以我按照文档中@WithUserDetails的说明来注释我的测试
我的测试类看起来像:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class, ConfigTest.class }, webEnvironment =
WebEnvironment.RANDOM_PORT)
@Transactional
public class SecurityIntegrationTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc=MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
}
@Test
@WithUserDetails(userDetailsServiceBeanName="myUserDetailsService")
public void testTemp() throws Exception {
this.mockMvc.perform( get("/")).andDo(print()).andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
Config 中的 UserDetails Bean 如下所示:
@Configuration
public class ConfigTest extends WebMvcConfigurerAdapter {
public ConfigTest() {
super();
}
@Bean
public UserDetailsService myUserDetailsService() {
return new MyUserDetailsService();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行测试,则会出现异常:
TestContextManager - …Run Code Online (Sandbox Code Playgroud) spring integration-testing unit-testing spring-mvc spring-security
当说"2013年3月"(没有日期)与"2013年3月17日"(有一天)这样的事情时,美丽的波兰语使用不同的语法作为月份的名称.
使用PHP strftime(),%B可以为无日案例提供正确的月份名称.如何在日常情况下正确写日期?我是否必须自己编写代码或是否已经支持此类情况?
我想将schema.org标记添加到产品页面.所以基本上所有页面都包含在:<div itemscope itemtype="http://schema.org/Product">
页面显示我标记的产品名称itemprop="name",它显示我标记的图像itemprop="image"等.
为了标记商品的价格和类别,我使用http://schema.org/Offer.问题是价格和类别显示在网页的不同部分.是否可以使用itemtype=http://schema.org/Offer两次,如下例所示?
<div itemscope itemtype="http://schema.org/Product">
<span itemprop="name">Name of product</span>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<span itemprop="category">animals</span>
</div>
<img itemprop="image" src="#"/>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<span itemprop="price">1000 EUR</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我试图在春天使用百里香叶来定位文本字符串.我的html模板位于/ src/main/resources/templates /
所以我有:
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
public MvcConfig() {
super();
}
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.addViewController("/sorting").setViewName("sorting");
}
@Override
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/","/resource/*");
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/assets/");
registry.addResourceHandler("/css/**")
.addResourceLocations("/css/");
}
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return …Run Code Online (Sandbox Code Playgroud) 我有一个简单的AlertDialog与setSingleChoiceItems列表的两个元素工作正常.
final CharSequence[] blackwhite = {"White", "Black"};
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Title");
alertDialogBuilder
.setCancelable(false)
.setSingleChoiceItems(blackwhite, -1,null)
.setPositiveButton("Start", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ListView lw = ((AlertDialog) dialog).getListView();
Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());
// Do something with checkedItem
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)
在其余代码中,这两个项实际上对应于枚举:
public enum Player {
WHITE, BLACK
}
Run Code Online (Sandbox Code Playgroud)
是否有一种优雅的方式直接在setSingleChoiceItems中使用枚举而无需手动转换为String/CharSequence?例如,如果我后来决定仅在枚举中将"WHITE"更改为"GREEN",则它也会自动显示在警告对话框中.
android ×3
java ×3
spring-mvc ×3
spring ×2
thymeleaf ×2
database ×1
date ×1
enums ×1
generics ×1
gettext ×1
listview ×1
localization ×1
microdata ×1
php ×1
random ×1
schema.org ×1
testing ×1
textwatcher ×1
translation ×1
unit-testing ×1