如何检查一个ID是否包含特定短语?

huangapple go评论71阅读模式
英文:

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 onClicks.

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 onClicks.

The button code is simple (removed most formatting code):

&lt;Button
           android:id=&quot;@+id/team1AddButton&quot;
           android:layout_width=&quot;55dp&quot;
           android:layout_height=&quot;75dp&quot;
           android:text=&quot;+&quot;
           android:onClick=&quot;addScore&quot; /&gt;

&lt;Button
           android:id=&quot;@+id/team1SubButton&quot;
           android:layout_width=&quot;55dp&quot;
           android:layout_height=&quot;75dp&quot;
           android:text=&quot;-&quot;
           android:onClick=&quot;addScore&quot; /&gt;

The Java code is what I'm having trouble with.

public void addScore(View v) {
        if (v.getId().toString().contains(&quot;team1&quot;));

    }

答案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(&quot;/&quot;);
    String result = parts[parts.length - 1];
    
    if (result.contains(&quot;team1&quot;)) {
        //..
    }
}

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(&quot;team1&quot;)) {
       //..
   }
}

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){
        
}

huangapple
  • 本文由 发表于 2023年4月10日 19:29:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定