在循环中编辑TextView

时间:2015-03-02 07:13:12

标签: java android while-loop textview

我正在尝试在循环中编辑TextViews。可以在循环外定义TextView,但必须在循环内编辑它们。我尝试将我的代码放在while循环中,但是当我运行它时应用程序崩溃了。 这是我的代码:

package com.example.ani.mapactivity;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Random;


public class LotA extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lot);
    LinearLayout toplayout = (LinearLayout) findViewById(R.id.toplayout);
    Random findnum = new Random();
    int i = findnum.nextInt(1-0) + 0;
    LinearLayout layout = (LinearLayout) toplayout.getChildAt(i);
    Random spotinit = new Random();
    int a = spotinit.nextInt(9-0) + 0;
    TextView spot = (TextView) layout.getChildAt(a);
    spot.setBackgroundColor(Color.RED);
}


@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_lot, 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);
}

}

这是我的XML代码:     

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:id="@+id/layout1"
    android:orientation="vertical">
    <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
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:id="@+id/layout2"
    android:orientation="vertical">
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot6"
        android:layout_marginTop="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot7"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot8"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot9"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot10"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:id="@+id/layout3"
    android:orientation="vertical">
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot11"
        android:layout_marginTop="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot12"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot13"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot14"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
    <TextView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id = "@+id/spot15"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"/>
</LinearLayout>

以下是我尝试将其置于while循环中的方法。我更喜欢while循环(因为我对此感到最舒服)但是for循环也没问题。

package com.example.ani.mapactivity;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Random;


public class LotA extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lot);
    LinearLayout toplayout = (LinearLayout) findViewById(R.id.toplayout);
    int loopnum = 1;
    while(loopnum <= 3){
         Random findnum = new Random();
         int i = findnum.nextInt(1-0) + 0;
         LinearLayout layout = (LinearLayout) toplayout.getChildAt(i);
         Random spotinit = new Random();
         int a = spotinit.nextInt(9-0) + 0;
         TextView spot = (TextView) layout.getChildAt(a);
         spot.setBackgroundColor(Color.RED);
         loopnum += 1;
    } 

}


@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_lot, 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);
}

}

如果我的代码需要澄清,请在评论中回复。我是新手,所以请尽量不对我的代码做出粗鲁的评论。谢谢你的帮助。

此致

阿尼

1 个答案:

答案 0 :(得分:0)

考虑到你有一个linearoutout,它是你的rootView,其id为topLayout

你的randomNumber限制对两者都是错误的。请参阅下面的代码..

 Random findnum = new Random();
 int i = findnum.nextInt(2); //Since you have 3 linear layouts 
 LinearLayout layout = (LinearLayout) toplayout.getChildAt(i);
 Random spotinit = new Random();
 int a = spotinit.nextInt(4) ;//Since each linear layout has 5 textViews
 TextView spot = (TextView) layout.getChildAt(a);
 spot.setBackgroundColor(Color.RED);
 loopnum += 1;