在android中动态更改变量TextView id
是否可以动态更改变量名。我的意思是我有4个文本视图,名字是:textView1,textView2,textView3,textView4 我将在for循环中添加一个计数,我希望执行以下操作:在android中动态更改变量TextView id,android,Android,是否可以动态更改变量名。我的意思是我有4个文本视图,名字是:textView1,textView2,textView3,textView4 我将在for循环中添加一个计数,我希望执行以下操作: for (int i; i<count; i++) { textView+(i).setText(Count) //textView1.setText(Count); //textView2.setText(Count); //textView3.setText(Count);
for (int i; i<count; i++)
{
textView+(i).setText(Count)
//textView1.setText(Count);
//textView2.setText(Count);
//textView3.setText(Count);
//textView4.setText(Count);
count++
}
for(int i;i)
不可能。这就是java的工作原理,可以称之为语法规则
上面的说明你能做什么
取一个集合
并迭代它。你不可能懒得去做
至少初始化变量名
不可能。这就是java的工作原理,可以称之为语法规则
上面的说明你能做什么
取一个集合
并迭代它。你不可能懒得去做
至少初始化变量名
不可能。这就是java的工作原理,可以称之为语法规则
上面的说明你能做什么
取一个集合
并迭代它。你不可能懒得去做
至少初始化变量名
不可能。这就是java的工作原理,可以称之为语法规则
上面的说明你能做什么
取一个集合
并迭代它。你不可能懒得去做
至少初始化变量名
基本上,你想要的不是最佳实践
但是如果您可以通过反射
String textViewName = "textView";
String methodName = "setText";
Class activityClass = this.getClass();
for(int i = 0; i < count; i++) {
try {
Field field = activityClass.getField(textViewName + i);
if(field != null){
Method method = field.getClass().getMethod(methodName, String.class);
method.invoke(field, Count);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
String textViewName=“textView”;
String methodName=“setText”;
类activityClass=this.getClass();
for(int i=0;i
基本上,您想要的不是最佳实践
但是如果您可以通过反射
String textViewName = "textView";
String methodName = "setText";
Class activityClass = this.getClass();
for(int i = 0; i < count; i++) {
try {
Field field = activityClass.getField(textViewName + i);
if(field != null){
Method method = field.getClass().getMethod(methodName, String.class);
method.invoke(field, Count);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
String textViewName=“textView”;
String methodName=“setText”;
类activityClass=this.getClass();
for(int i=0;i
基本上,您想要的不是最佳实践
但是如果您可以通过反射
String textViewName = "textView";
String methodName = "setText";
Class activityClass = this.getClass();
for(int i = 0; i < count; i++) {
try {
Field field = activityClass.getField(textViewName + i);
if(field != null){
Method method = field.getClass().getMethod(methodName, String.class);
method.invoke(field, Count);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
String textViewName=“textView”;
String methodName=“setText”;
类activityClass=this.getClass();
for(int i=0;i
基本上,您想要的不是最佳实践
但是如果您可以通过反射
String textViewName = "textView";
String methodName = "setText";
Class activityClass = this.getClass();
for(int i = 0; i < count; i++) {
try {
Field field = activityClass.getField(textViewName + i);
if(field != null){
Method method = field.getClass().getMethod(methodName, String.class);
method.invoke(field, Count);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
String textViewName=“textView”;
String methodName=“setText”;
类activityClass=this.getClass();
for(int i=0;i
在运行时无法更改变量的名称。变量是开发人员用来告诉“计算机”您希望它做什么的工具。这个问题与询问您是否可以编写无需添加分号的代码一样无效(;
)在每个语句的末尾。变量名就是您调用正在使用的某些元素的名称。这就是Java的工作方式
也就是说,可以通过使用一个简单的数组并对其进行迭代来实现您正在尝试的操作。以下是您可以实现的方法:
TextView[] textViews = new TextView[] { textView1, textView2, textView3, textView4 };
for (int i = 0; i < textViews.length; i++)
{
textViews[i].setText("some text");
}
在运行时无法更改变量的名称。变量是作为开发人员用来告诉“计算机”您希望它做什么的工具。这个问题与询问您是否可以编写不需要添加分号的代码一样无效(;
)在每个语句的末尾。变量名就是您调用正在使用的某些元素的名称。这就是Java的工作方式
也就是说,可以通过使用一个简单的数组并对其进行迭代来实现您正在尝试的操作。以下是您可以实现的方法:
TextView[] textViews = new TextView[] { textView1, textView2, textView3, textView4 };
for (int i = 0; i < textViews.length; i++)
{
textViews[i].setText("some text");
}
在运行时无法更改变量的名称。变量是作为开发人员用来告诉“计算机”您希望它做什么的工具。这个问题与询问您是否可以编写不需要添加分号的代码一样无效(;
)在每一条语句的末尾。变量名就是您调用正在使用的某些元素的名称。这就是简单的方法