Java 如何根据变量查找id?
我的应用程序中有11个文本视图。它们的ID都是text_1,text_2 我还有一个名为start的int,它是一个介于1和11之间的数字 我已将TextView的变量初始化为text1、text2等,但尚未分配它们的ID 现在,我希望text1(变量)应该被赋值为id R.id.text_(start的值) 怎么做 以下是我目前的代码:Java 如何根据变量查找id?,java,android,android-studio,Java,Android,Android Studio,我的应用程序中有11个文本视图。它们的ID都是text_1,text_2 我还有一个名为start的int,它是一个介于1和11之间的数字 我已将TextView的变量初始化为text1、text2等,但尚未分配它们的ID 现在,我希望text1(变量)应该被赋值为id R.id.text_(start的值) 怎么做 以下是我目前的代码: public class MainActivity extends AppCompatActivity { TextView text1; T
public class MainActivity extends AppCompatActivity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
TextView text7;
TextView text8;
TextView text9;
TextView text10;
TextView text11;
private placeNames[] placeBank = new placeNames[]{
new placeNames(R.string.place1, 5),
new placeNames(R.string.place2, 6),
new placeNames(R.string.place3, 5),
new placeNames(R.string.place4, 6),
new placeNames(R.string.place5, 7)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newGame(View view){
int randomNum = Integer.parseInt(String.valueOf(Math.random() * 6));
String placeName = getPlaceData(randomNum)[0];
int placeLength = Integer.parseInt(getPlaceData(randomNum)[1]);
ArrayList<String> letters = null;
for (int i = 1; i>=placeName.length();i++){
letters.add(String.valueOf(placeName.charAt(i)));
}
String start = String.valueOf(getStartView(placeLength));
text1 = findViewById(R.id.)
}
public String[] getPlaceData(int id){
int length = placeBank[id].getLength();
String name = String.valueOf(placeBank[id].getPlaceId());
String[] result = {name, String.valueOf(length)};
return result;
}
public int getStartView(int length){
int result = (11 - length)/2;
return result;
}
}
public类MainActivity扩展了AppCompatActivity{
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
TextView text7;
TextView text8;
TextView text9;
TextView text10;
TextView text11;
私人地名[]placeBank=新地名[]{
新地名(R.string.place1,5),
新地名(R.string.place2,6),
新地名(R.string.place3,5),
新地名(R.string.place4,6),
新地名(R.string.place5,7)
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
公共无效新游戏(视图){
int randomNum=Integer.parseInt(String.valueOf(Math.random()*6));
字符串placeName=getPlaceData(randomNum)[0];
int placeLength=Integer.parseInt(getPlaceData(randomNum)[1]);
ArrayList字母=null;
对于(inti=1;i>=placeName.length();i++){
字母.add(String.valueOf(placeName.charAt(i));
}
String start=String.valueOf(getStartView(placeLength));
text1=findViewById(R.id.)
}
公共字符串[]getPlaceData(int id){
int length=placeBank[id].getLength();
String name=String.valueOf(placeBank[id].getPlaceId());
字符串[]结果={name,String.valueOf(length)};
返回结果;
}
公共整数getStartView(整数长度){
int结果=(11-长度)/2;
返回结果;
}
}
我不确定这是否是您需要的,但根据您的问题,我想这就是:
public class MainActivity extends AppCompatActivity {
private int start = 2; //Any int number from 1 to 11
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private TextView text5;
private TextView text6;
private TextView text7;
private TextView text8;
private TextView text9;
private TextView text10;
private TextView text11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch (start){
case 1:
text1 = findViewById(R.id.text_1);
break;
case 2:
text2 = findViewById(R.id.text_2);
break;
case 3:
text3 = findViewById(R.id.text_3);
break;
case 4:
text4 = findViewById(R.id.text_4);
break;
case 5:
text5 = findViewById(R.id.text_5);
break;
case 6:
text6 = findViewById(R.id.text_6);
break;
case 7:
text7 = findViewById(R.id.text_7);
break;
case 8:
text8 = findViewById(R.id.text_8);
break;
case 9:
text9 = findViewById(R.id.text_9);
break;
case 10:
text10 = findViewById(R.id.text_10);
break;
case 11:
text11 = findViewById(R.id.text_11);
break;
}
}
}我不确定这是否是您需要的,但根据您的问题,我想这就是:
public class MainActivity extends AppCompatActivity {
private int start = 2; //Any int number from 1 to 11
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private TextView text5;
private TextView text6;
private TextView text7;
private TextView text8;
private TextView text9;
private TextView text10;
private TextView text11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch (start){
case 1:
text1 = findViewById(R.id.text_1);
break;
case 2:
text2 = findViewById(R.id.text_2);
break;
case 3:
text3 = findViewById(R.id.text_3);
break;
case 4:
text4 = findViewById(R.id.text_4);
break;
case 5:
text5 = findViewById(R.id.text_5);
break;
case 6:
text6 = findViewById(R.id.text_6);
break;
case 7:
text7 = findViewById(R.id.text_7);
break;
case 8:
text8 = findViewById(R.id.text_8);
break;
case 9:
text9 = findViewById(R.id.text_9);
break;
case 10:
text10 = findViewById(R.id.text_10);
break;
case 11:
text11 = findViewById(R.id.text_11);
break;
}
}
}我从您的问题中了解到的是,您不希望在使用switch/when cases时使用switch/when,并且希望保持代码简短、干净和简单 为此,我建议您使用
FindViewWithTag
,因为您可以通过编程方式在其中传递字符串方法如下:
首先,为您拥有的每个视图设置标记,就像使用android:tag=“yourtag”
设置ID一样
然后,您可以在代码中将其初始化为:
String str = "MyTag1" //or for 2nd TextView "MyTag2", "MyTag3"
TextView textView = view.findViewWithTag(string);
现在,这个TextView将在不使用swtich case的情况下进行初始化,但请记住将此标记的格式与存储在XML文件中的匹配。此外,使用计数器类型机制将此后缀整数更改为TextView 11的TextView 11
一些提示:- 此findViewWithTag使用视图,您不能直接调用finyViewWithTag,因此在本例中,您有一个视图,但在其他任何地方,您都必须将其父元素初始化为view
- 此外,您可以使用字符串资源分配标记,就像您可以使用xml中的字符串资源分配标记一样,然后可以像这样调用它
if(i==7) { String tag = getResources().getString(R.string.myTag7); textview = view.finyViewWithTag(tag); }
FindViewWithTag
,因为您可以通过编程方式在其中传递字符串方法如下:
首先,为您拥有的每个视图设置标记,就像使用android:tag=“yourtag”
设置ID一样
然后,您可以在代码中将其初始化为:
String str = "MyTag1" //or for 2nd TextView "MyTag2", "MyTag3"
TextView textView = view.findViewWithTag(string);
现在,这个TextView将在不使用swtich case的情况下进行初始化,但请记住将此标记的格式与存储在XML文件中的匹配。此外,使用计数器类型机制将此后缀整数更改为TextView 11的TextView 11
一些提示:- 此findViewWithTag使用视图,您不能直接调用finyViewWithTag,因此在本例中,您有一个视图,但在其他任何地方,您都必须将其父元素初始化为view
- 此外,您可以使用字符串资源分配标记,就像您可以使用xml中的字符串资源分配标记一样,然后可以像这样调用它
if(i==7) { String tag = getResources().getString(R.string.myTag7); textview = view.finyViewWithTag(tag); }