Java 为同一事物执行多个if语句的更简单方法
对,所以我的问题是我有18个微调器,其中包含43个选项供用户选择。我希望每个选项在不同页面上更改一个链接,以便用户查看有关该选项的pdf。我已经有了共享的首选项来共享选择,我现在正在实现我的if/else if语句,但是如果我使用if/else语句,我将为每个微调器设置43个,然后为每个微调器设置43个,最终我需要实现700多条if语句,必须有一种更简单的方法来实现我的想法,我不知道。下面是一个代码示例:Java 为同一事物执行多个if语句的更简单方法,java,android,if-statement,Java,Android,If Statement,对,所以我的问题是我有18个微调器,其中包含43个选项供用户选择。我希望每个选项在不同页面上更改一个链接,以便用户查看有关该选项的pdf。我已经有了共享的首选项来共享选择,我现在正在实现我的if/else if语句,但是如果我使用if/else语句,我将为每个微调器设置43个,然后为每个微调器设置43个,最终我需要实现700多条if语句,必须有一种更简单的方法来实现我的想法,我不知道。下面是一个代码示例: if (spinnerValue == 1) { TextView
if (spinnerValue == 1)
{
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>";
textView.setText(Html.fromHtml(text));
}
else if (spinnerValue == 2){
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>";
textView.setText(Html.fromHtml(text));
}
else if (spinnerValue == 3){
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-3-Information-Systems.pdf'>Unit 3: Information Systems</a>";
textView.setText(Html.fromHtml(text));
}
if(spinnerValue==1)
{
TextView TextView=(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(真);
setMovementMethod(LinkMovementMethod.getInstance());
字符串文本=”;
textView.setText(Html.fromHtml(text));
}
else if(spinnerValue==2){
TextView TextView=(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(真);
setMovementMethod(LinkMovementMethod.getInstance());
字符串文本=”;
textView.setText(Html.fromHtml(text));
}
else if(spinnerValue==3){
TextView TextView=(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(真);
setMovementMethod(LinkMovementMethod.getInstance());
字符串文本=”;
textView.setText(Html.fromHtml(text));
}
因此,在这最后,将有43个if(spinnervalue==1到43)和18个spinnervalue。我对java和android studio非常陌生,因此非常感谢您提供的任何帮助或指导,或者对此进行解释
编辑:我的if语句可以工作,开关也可以,但是我仍然需要实现43个if/switch语句18次。有没有什么方法可以让我按照-if(spinnervalue 1到18==1)的思路来做一些事情,这样我就可以一次性编写43个案例,并将其应用于所有spinnervalue(1-18)很多都可以重构,例如
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "";
switch(spinnerValue) {
case 1:
text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>";
break;
case 2:
text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>";
break;
}
textView.setText(Html.fromHtml(text));
TextView TextView=(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(真);
setMovementMethod(LinkMovementMethod.getInstance());
字符串文本=”;
开关(spinnerValue){
案例1:
text=“”;
打破
案例2:
text=“”;
打破
}
textView.setText(Html.fromHtml(text));
但我不相信这真的是最好的方法。我可能会创建一个微调器值->URL的映射(或者一个数组,因为它只是键的一个数值),并使用它来填充链接。将需要更少的代码行,并且在未来具有更多的可扩展性 其中很多都可以重构,例如
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "";
switch(spinnerValue) {
case 1:
text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>";
break;
case 2:
text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>";
break;
}
textView.setText(Html.fromHtml(text));
TextView TextView=(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(真);
setMovementMethod(LinkMovementMethod.getInstance());
字符串文本=”;
开关(spinnerValue){
案例1:
text=“”;
打破
案例2:
text=“”;
打破
}
textView.setText(Html.fromHtml(text));
但我不相信这真的是最好的方法。我可能会创建一个微调器值->URL的映射(或者一个数组,因为它只是键的一个数值),并使用它来填充链接。将需要更少的代码行,并且在未来具有更多的可扩展性 以下是我对此的初步想法:
String[] fileNames = new String[] {
//Put your filenames here in order
}
然后在当前代码中:
if(spinnerValue<=fileNames.length) { //Make sure the value is valid
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/"+fileNames[spinnerValue-1]+".pdf'>"+fileNames[spinnerValue-1].replaceAll("-"," ")+"</a>";
textView.setText(Html.fromHtml(text));
}
if(spinnerValue以下是我对此的初步想法:
String[] fileNames = new String[] {
//Put your filenames here in order
}
然后在当前代码中:
if(spinnerValue<=fileNames.length) { //Make sure the value is valid
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/"+fileNames[spinnerValue-1]+".pdf'>"+fileNames[spinnerValue-1].replaceAll("-"," ")+"</a>";
textView.setText(Html.fromHtml(text));
}
if(spinnerValue因为唯一的区别是字符串,并且您使用的是连续值1-43,所以最简单的方法是文本数组
请记住,数组是基于零的
private static final String[]微调器\u text={
/*1*/"",
/*2*/"",
/*3*/""
};
TextView TextView=(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(真);
setMovementMethod(LinkMovementMethod.getInstance());
setText(Html.fromHtml(SPINNER_text[spinnerValue-1]);
因为唯一的区别是字符串,并且您使用的是连续值1-43,所以最简单的方法是文本数组
请记住,数组是基于零的
private static final String[]微调器\u text={
/*1*/"",
/*2*/"",
/*3*/""
};
TextView TextView=(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(真);
setMovementMethod(LinkMovementMethod.getInstance());
setText(Html.fromHtml(SPINNER_text[spinnerValue-1]);
您可以创建如下映射表:
Map<Integer, String> spinnerValueToLinkMapping = new HashMap<Integer, String> {{
put(1, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>");
put(2, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>");
put(3, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-3-Information-Systems.pdf'>Unit 3: Information Systems</a>");
}}
这样,您将得到易于支持的映射表和干代码。您可以创建如下映射表:
Map<Integer, String> spinnerValueToLinkMapping = new HashMap<Integer, String> {{
put(1, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>");
put(2, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>");
put(3, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-3-Information-Systems.pdf'>Unit 3: Information Systems</a>");
}}
有了这个,你就可以很容易地支持映射表和干代码
使用开关状态,实际上你可以考虑使用一个SwitcI尝试过,但是它看起来很相似,最后还是需要我为18个自旋体实现43个例子。你可以从一个数组中获取一个数组的值,只需把文本行放在一个数组/数组列表中,那么你就不需要一个if或一个开关,只需要选择适当的字符串值。实际上,你可以考虑使用一个SwitcI尝试过,但是它看起来很相似,最终还是会和我在一起。需要为18个微调器实现43个case。因为唯一改变的是值,所以您可以创建一个接受字符串的方法,并且根据具体情况,您可以从数组中获取值。如何将文本行放置在数组/数组列表中?这样您就不会