可以将对象参数作为ID传递到按ID查找视图中吗?

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

Is it possible to pass an object parameter as an id in the find view by id

问题

这是我自然而然地完成这个任务的方式,还有其他可能的可行方法吗?

public void error_checker(String Text_ID, int id, int id_2, String greater_num) {
    if (id > id_2) {
        TextView TextView = findViewById(R.id.Text_ID);
        TextView.setText("你的答案不能大于" + (greater_num));
    } else {

    }
}
英文:

Here is how I would naturally do this, are there any other possible ways that work?

public void error_checker(String Text_ID, int id,int id_2,String greater_num){
    if (id>id_2){


        TextView TextView = findViewById(R.id.Text_ID);

        TextView.setText(Integer.parseInt("You Answer Can't be Greater Than"+(greater_num));
    } else {

    }
}

答案1

得分: 0

你可以像这样做:

  TextView textView = findViewById(getResources().getIdentifier(Text_ID, "id", getPackageName()));

其中,Text_ID 是你的视图的ID(不带R.id.前缀)。

英文:

You can do it like this:

  TextView textView = findViewById(getResources().getIdentifier(Text_ID, "id", getPackageName()););

Where Text_ID is the ID of your view (without the R.id. prefix)

huangapple
  • 本文由 发表于 2020年8月19日 01:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63473539.html
匿名

发表评论

匿名网友

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

确定