我对Android的Java上的ID有问题.

我试过这么做,但没用.

LinearLayout ll;
int id = 2000000;
private void init() {
    ll = findViewById(R.id.layout);
}
private TextView addTV(int x, int y, int width, int height) {
    TextView tv = new TextView(this);
    tv.setId(id);
    id++;
    tv.setText("Hello");
    return tv;
}
private void changeTextTV(int id, String text) {
    TextView tv = findViewById(id); //<- this is wrong, but I don't know what is right
    tv.setText(text);
}
init();
ll.addView(addTV(0,0,100,100));
changeTextTV(2000000, "It's me");

你能帮我吗?

我是法国人,很抱歉我的拼写错误

推荐答案

经过长时间的搜索和测试.我找到了一些答案,你有两个 Select 来通过设置ID获得组件:

如果你想要一个硬身份证:

@SuppressLint("ResourceType")
private void changeTextTV(String text) {
    TextView tv = findViewById(2000000);
    tv.setText(text);
}

或者通过变量

private void changeTextTV(int id, String text) {
    TextView tv = findViewById(id);
    tv.setText(text);
}

谢谢你为我花时间.

Java相关问答推荐

具有额外列的Hibert多对多关系在添加关系时返回NonUniqueHealthExcellent

Listview—在Android Java中正确链接项目时出错

屏蔽字母数字代码的Java正则表达式

Character::Emoji不支持带数字的字符吗?

Java函数式编程中的双值单值映射

如何确定springboot在将json字段转换为Dto时如何处理它?

Java中的死锁及其重入锁和锁

Bean定义不是从Spring ApplationConext.xml文件加载的

对于亚洲/香港,使用ResolverStyle.STRICT的LocalDate.parse返回意外结果

如何在Spring Java中从数据库列中获取JSON中的具体数据

使SLF4J在Android中登录到Logcat,在测试中登录到控制台(Gradle依赖问题)

Instancio未在日志(log)中显示测试失败消息

Java组件项目中的JavaFX对话框国际化

对从Spring Boot 3.1.5升级到3.2.0的方法的查询验证失败

泛型与泛型问题的完美解决方案?

如何通过Java java.lang.Foreign API访问本机字节数组

验证没有';t work on Hibernate Entity';s字段

读取ConcurrentHashMap中的可变对象

如何使用java区分以下结果

单例模式中热切初始化和惰性初始化的区别