如何随机创建和分配变量并缩短 if-else 语句 (Java android)
How to randomly create and assign variables and shorten an if-else statement (Java android)
我想知道如何通过为 TextView
自动创建和分配变量来缩短我的代码。另外,如何缩短这些 if-else
语句?我看过其他线程,但它们没有满足我的需求。为此,我正在使用 Android Studio。
这是我的 Java 代码:
package com.example.ani.testproject;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainScreen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
final TextView spot1 = (TextView) findViewById(R.id.spot1);
final TextView spot2 = (TextView) findViewById(R.id.spot2);
final TextView spot3 = (TextView) findViewById(R.id.spot3);
final TextView spot4 = (TextView) findViewById(R.id.spot4);
final TextView spot5 = (TextView) findViewById(R.id.spot5);
final TextView spot6 = (TextView) findViewById(R.id.spot6);
final TextView spot7 = (TextView) findViewById(R.id.spot7);
final TextView spot8 = (TextView) findViewById(R.id.spot8);
final TextView spot9 = (TextView) findViewById(R.id.spot9);
final TextView spot10 = (TextView) findViewById(R.id.spot10);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
int loopnum = 1;
while(loopnum<4){
Random r = new Random();
int spotnumber = r.nextInt(11-1) + 1;
if(spotnumber==1){
spot1.setBackgroundColor(Color.RED);
}
else if(spotnumber==2){
spot2.setBackgroundColor(Color.RED);
}
else if(spotnumber==3){
spot3.setBackgroundColor(Color.RED);
}
else if(spotnumber==4){
spot4.setBackgroundColor(Color.RED);
}
else if(spotnumber==5){
spot5.setBackgroundColor(Color.RED);
}
else if(spotnumber==6){
spot6.setBackgroundColor(Color.RED);
}
else if(spotnumber==7){
spot7.setBackgroundColor(Color.RED);
}
else if(spotnumber==8){
spot8.setBackgroundColor(Color.RED);
}
else if(spotnumber==9){
spot9.setBackgroundColor(Color.RED);
}
else if(spotnumber==10){
spot10.setBackgroundColor(Color.RED);
}
loopnum += 1;
SystemClock.sleep(5000);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我不是很清楚,但是最后一个括号对应于中的括号:
public class MainScreen extends ActionBarActivity {
这是我的 XML 代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "horizontal">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="170dp"
android:layout_height="match_parent"
android:orientation = "vertical"
android:layout_marginTop = "5dp"
android:layout_marginRight = "5dp"
android:id="@+id/linearLayout">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot1"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot2"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot3"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot4"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot5"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="170dp"
android:layout_height="match_parent"
android:orientation = "vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginTop = "5dp"
android:id="@+id/LinearLayout2">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"
android:id="@+id/button"
android:layout_gravity="right" />
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot6"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot7"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot8"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot9"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot10"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
</LinearLayout>
请帮帮我!我是 Android 和 Java 的新手。
如果你想缩短你的 if => else if 语句,你可以使用 switch 语句。
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
基本上你给开关一个变量,然后根据给定变量的值给出一组案例场景。在您的情况下,它看起来像这样:
switch (spotnumber) {
case 1 : //case scenario with spotnumber = 1
spot1.setBackgroundColor(Color.RED);
break;
case 2 : //case scenario with spotnumber = 2
spot2.setBackgroundColor(Color.RED);
break;
}
不要忘记"break"否则匹配场景后的每条语句都会被执行
看起来保留 TextView
的列表而不是每个变量的单独变量对您的情况来说是合适的。我们可以使用 ArrayList
,并通过从布局 xml.
中指定的名称中检索 TextView
的 ID 来填充它
ArrayList<TextView> spots = new ArrayList<TextView>();
for(int i = 1; i < 11; i++) {
int id = getResources().getIdentifier("spot" + i, "id", getPackageName());
spots.add((TextView) findViewById(id));
}
这将大大简化代码的其他部分,因为我们现在根本不需要任何 if
。
Random r = new Random();
int spotnumber = r.nextInt(10);
spots.get(spotnumber).setBackgroundColor(Color.RED);
一个可能的解决方案是使用依赖性 Injection.You 可以查看 RoboGuice。
但是 Android 最佳实践不推荐此 method.You 可能只是自动化您的功能并使用数组列表存储 textViews。
List<TextView> spotList= new ArrayList<TextView>();
for(int i = 1; i <= 10; i++) {
int id = getResources().getIdentifier("spot" + i, "id", getPackageName());
spotList.add((TextView) findViewById(id));
}
并且 if-else 语句将使用 switch case 进行修改:
switch (spotnumber) {
case 1 : //equivalent of spotnumber==1
spot1.setBackgroundColor(Color.RED);
break;
case 2 : //equivalent of if spotnumber==2
spot2.setBackgroundColor(Color.RED);
break;
.
.
.
}
我想知道如何通过为 TextView
自动创建和分配变量来缩短我的代码。另外,如何缩短这些 if-else
语句?我看过其他线程,但它们没有满足我的需求。为此,我正在使用 Android Studio。
这是我的 Java 代码:
package com.example.ani.testproject;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainScreen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
final TextView spot1 = (TextView) findViewById(R.id.spot1);
final TextView spot2 = (TextView) findViewById(R.id.spot2);
final TextView spot3 = (TextView) findViewById(R.id.spot3);
final TextView spot4 = (TextView) findViewById(R.id.spot4);
final TextView spot5 = (TextView) findViewById(R.id.spot5);
final TextView spot6 = (TextView) findViewById(R.id.spot6);
final TextView spot7 = (TextView) findViewById(R.id.spot7);
final TextView spot8 = (TextView) findViewById(R.id.spot8);
final TextView spot9 = (TextView) findViewById(R.id.spot9);
final TextView spot10 = (TextView) findViewById(R.id.spot10);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
int loopnum = 1;
while(loopnum<4){
Random r = new Random();
int spotnumber = r.nextInt(11-1) + 1;
if(spotnumber==1){
spot1.setBackgroundColor(Color.RED);
}
else if(spotnumber==2){
spot2.setBackgroundColor(Color.RED);
}
else if(spotnumber==3){
spot3.setBackgroundColor(Color.RED);
}
else if(spotnumber==4){
spot4.setBackgroundColor(Color.RED);
}
else if(spotnumber==5){
spot5.setBackgroundColor(Color.RED);
}
else if(spotnumber==6){
spot6.setBackgroundColor(Color.RED);
}
else if(spotnumber==7){
spot7.setBackgroundColor(Color.RED);
}
else if(spotnumber==8){
spot8.setBackgroundColor(Color.RED);
}
else if(spotnumber==9){
spot9.setBackgroundColor(Color.RED);
}
else if(spotnumber==10){
spot10.setBackgroundColor(Color.RED);
}
loopnum += 1;
SystemClock.sleep(5000);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我不是很清楚,但是最后一个括号对应于中的括号:
public class MainScreen extends ActionBarActivity {
这是我的 XML 代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "horizontal">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="170dp"
android:layout_height="match_parent"
android:orientation = "vertical"
android:layout_marginTop = "5dp"
android:layout_marginRight = "5dp"
android:id="@+id/linearLayout">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot1"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot2"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot3"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot4"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot5"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="170dp"
android:layout_height="match_parent"
android:orientation = "vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginTop = "5dp"
android:id="@+id/LinearLayout2">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"
android:id="@+id/button"
android:layout_gravity="right" />
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot6"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot7"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot8"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot9"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot10"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
</LinearLayout>
请帮帮我!我是 Android 和 Java 的新手。
如果你想缩短你的 if => else if 语句,你可以使用 switch 语句。
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
基本上你给开关一个变量,然后根据给定变量的值给出一组案例场景。在您的情况下,它看起来像这样:
switch (spotnumber) {
case 1 : //case scenario with spotnumber = 1
spot1.setBackgroundColor(Color.RED);
break;
case 2 : //case scenario with spotnumber = 2
spot2.setBackgroundColor(Color.RED);
break;
}
不要忘记"break"否则匹配场景后的每条语句都会被执行
看起来保留 TextView
的列表而不是每个变量的单独变量对您的情况来说是合适的。我们可以使用 ArrayList
,并通过从布局 xml.
TextView
的 ID 来填充它
ArrayList<TextView> spots = new ArrayList<TextView>();
for(int i = 1; i < 11; i++) {
int id = getResources().getIdentifier("spot" + i, "id", getPackageName());
spots.add((TextView) findViewById(id));
}
这将大大简化代码的其他部分,因为我们现在根本不需要任何 if
。
Random r = new Random();
int spotnumber = r.nextInt(10);
spots.get(spotnumber).setBackgroundColor(Color.RED);
一个可能的解决方案是使用依赖性 Injection.You 可以查看 RoboGuice。 但是 Android 最佳实践不推荐此 method.You 可能只是自动化您的功能并使用数组列表存储 textViews。
List<TextView> spotList= new ArrayList<TextView>();
for(int i = 1; i <= 10; i++) {
int id = getResources().getIdentifier("spot" + i, "id", getPackageName());
spotList.add((TextView) findViewById(id));
}
并且 if-else 语句将使用 switch case 进行修改:
switch (spotnumber) {
case 1 : //equivalent of spotnumber==1
spot1.setBackgroundColor(Color.RED);
break;
case 2 : //equivalent of if spotnumber==2
spot2.setBackgroundColor(Color.RED);
break;
.
.
.
}