java - 如何随机创建和分配变量并缩短 if-else 语句 (Java android)

标签 java android variable-assignment

我想知道如何通过为 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 的新手。

最佳答案

看起来保留 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);

关于java - 如何随机创建和分配变量并缩短 if-else 语句 (Java android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28689275/

相关文章:

python - 是否可以在 Python 中进行多行分配?

java - Java中的字符序列化、BufferedReader

Java无法处理π字符

java - 用于 Java 的类 PHP 表示语言

java - 清除 Android 中 SharedPreferences 中的首选项,而不仅仅是值

c# - 在 C# 中,字符串类型如何传递给方法或分配给变量?

r - 在 R 中,如何使用索引修改/重新分配列表元素?

java - 复选框在选中时不会禁用,android studio

javascript - 如何使用Cordova管理Android手机的文件系统

android - android中ListView行的动态高度