英文:
How do I check if an ID contains a certain phrase?
问题
I am making a simple app with four scores, each score having an add button and a subtract button. To make code simpler, I named each button's onClick
to addScore
or subScore
. Each button's ID contains something such as android:@+id/team1AddButton
. I need to check in the onClick
which team is being affected for both onClick
s.
The button code is simple (removed most formatting code):
<Button
android:id="@+id/team1AddButton"
android:layout_width="55dp"
android:layout_height="75dp"
android:text="+"
android:onClick="addScore" />
<Button
android:id="@+id/team1SubButton"
android:layout_width="55dp"
android:layout_height="75dp"
android:text="-"
android:onClick="addScore" />
The Java code is what I'm having trouble with.
public void addScore(View v) {
if (v.getId().toString().contains("team1"));
}
英文:
I am making a simple app with four scores, each score having an add button and a subtract button. To make code simpler, I named each button's onClick
to addScore
or subScore
. Each button's ID contains something such as android:@+id/team1AddButton
. I need to check in the onClick
which team is being affected for both onClick
s.
The button code is simple (removed most formatting code):
<Button
android:id="@+id/team1AddButton"
android:layout_width="55dp"
android:layout_height="75dp"
android:text="+"
android:onClick="addScore" />
<Button
android:id="@+id/team1SubButton"
android:layout_width="55dp"
android:layout_height="75dp"
android:text="-"
android:onClick="addScore" />
The Java code is what I'm having trouble with.
public void addScore(View v) {
if (v.getId().toString().contains("team1"));
}
答案1
得分: 1
这是你要找的内容:
public void addScore(View v) {
String resourceName = getResources().getResourceName(v.getId());
String[] parts = resourceName.split("/");
String result = parts[parts.length - 1];
if (result.contains("team1")) {
//..
}
}
resourceName的值将类似于:packageName:id/team1AddButton
所以如果我们通过"/"拆分字符串并取最后一部分,就可以得到结果。
或者
```java
public void addScore(View v) {
String resourceName = getResources().getResourceName(v.getId());
if (resourceName.contains("team1")) {
//..
}
}
如果不需要精确的resourceName,只需检查是否包含即可。
希望对你有所帮助!
英文:
Here is what are you looking for:
public void addScore(View v) {
String resourceName = getResources().getResourceName(v.getId());
String[] parts = resourceName.split("/");
String result = parts[parts.length - 1];
if (result.contains("team1")) {
//..
}
}
resourceName value will be like: packageName:id/team1AddButton
So if we split the string by "/" and take the last part, we get the result.
or
public void addScore(View v) {
String resourceName = getResources().getResourceName(v.getId());
if (resourceName.contains("team1")) {
//..
}
}
If exact resourceName doesn't required then just check if contains or not.
Hope it helps!
答案2
得分: 0
你无法使用字符串类型获取视图的ID。如果调用view.getId(),它将返回一个整数值。
尝试这样做
if(view.getId() == R.id.team1AddButton){
}else if(view.getId() == R.id.team1SubButton){
}
英文:
You can't get view's id with string type.If you call view.getId(),it'll return an Integer value.
try this
if(view.getId() == R.id.team1AddButton){
}else if(view.getId() == R.id.team1SubButton){
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论