基本上,我试图创建一个按钮,单击时(注意:不是按下)会改变颜色从颜色1至COLOR2.再次单击时,它将从color2更改为color1.
我疯狂搜索,我设法提取的唯一信息是按下按钮时更改颜色的方式,即当用户按下按钮时(此代码将在下面写).但是,我希望在用户单击(按下并释放)按钮时更改颜色,然后在用户再次单击后再更改颜色.
此文件位于res/drawable中
<!-- Changes color when user hols down button -->
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<!-- change to color2 -->
</shape>
</item>
<item>
<shape>
<!-- change to color1 -->
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud) 我有功能
private Class readObject (ObjectInput in, Class objectclass){
try {
Object o = in.readObject ();
if (o instanceof objectclass) {
return (objectclass)o;
}
} catch (Exception e) {
}
Run Code Online (Sandbox Code Playgroud)
在我的课上,我用以下方法调用此函数:
ObjectInput in = (..)
Type1 type = readObject (in, Type1.class);
(do something with type)
while(true){
Type2 type2 = readObject (in, Type2.class);
(do something with type2)
Run Code Online (Sandbox Code Playgroud)
基本上,readObject应该返回我作为参数的类,如果这是Object o的正确类型.您可以将其视为,我正在阅读的Type1,Type2和Type3对象,但如果我遇到Type3,我什么都不做.
我写的这段代码不能正常工作.例如,
Type1 type = readObject (in, Type1.class);
Run Code Online (Sandbox Code Playgroud)
给我警告"无法从Class转换为Type1".
我有一个字符串:
testString = """ My name is %s
and I am %s years old
and I live in %s"""
Run Code Online (Sandbox Code Playgroud)
我有代码可以找到我想输入到 testString 中的这三个字符串。我怎么做?在 Java 中,我会这样做:
return String.format(testString, string1, string2, string3)
Run Code Online (Sandbox Code Playgroud)
Python中是否有等价物?