Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据变量查找id?_Java_Android_Android Studio - Fatal编程技术网

Java 如何根据变量查找id?

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

我的应用程序中有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;
    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);
    }
    

我从您的问题中了解到的是,您不希望在使用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);
    }
    

这不是一个写得特别好的问题,因此我建议您阅读,不可能用给出的信息正确回答您的问题。这就是为什么没有人回答。另外,我建议你做一点调查,因为我只需搜索“TextView Android Studio”就可以在TextView上立即找到文档。编辑:您还应该添加一些示例代码。@KNOBPersonal请查看编辑过的问题告诉我是否理解您的意思。您希望从Activity Java类而不是xml中指定TextView的ID???@LuisAguilar ID仅在xml中,但要指定哪个ID,这必须根据int start的值来决定。@DeathVenom您能提供您到目前为止所编写的代码吗?这不是一个写得特别好的问题,因此我建议您阅读这篇文章,用给出的信息来正确回答您的问题是不可能的。这就是为什么没有人回答。另外,我建议你做一点调查,因为我只需搜索“TextView Android Studio”就可以在TextView上立即找到文档。编辑:您还应该添加一些示例代码。@KNOBPersonal请查看编辑过的问题告诉我是否理解您的意思。您想从Activity Java类而不是xml类中指定TextView的ID???@LuisAguilar ID仅在xml中,但要指定哪个ID,这必须根据int start的值来决定。@DeathVenom您能提供您到目前为止编写的代码吗?是的,这是一个选项。然而,我正在寻找一个更短更有效的方法