如何在不使用ID的情况下从视图中获取价值?

时间:2014-07-20 14:50:11

标签: java android xml

我的XML文件中有7个结果视图,我希望控制它们(从中获取值)而不分别在每个视图上声明,我想在每个视图上执行循环(for循环)并存储值在数组中,所以任何人都可以帮我解决我遇到的问题? 在此先感谢您的帮助。 这是我的XML代码:

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <com.example.loto.ViewLoto
        android:id="@+id/viewLoto1"
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" >
    </com.example.loto.ViewLoto>

    <com.example.loto.ViewLoto
        android:id="@+id/viewLoto2"
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" >
    </com.example.loto.ViewLoto>

    <com.example.loto.ViewLoto
        android:id="@+id/viewLoto3"
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" >
    </com.example.loto.ViewLoto>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <com.example.loto.ViewLoto
        android:id="@+id/viewLoto4"
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" >
    </com.example.loto.ViewLoto>

    <com.example.loto.ViewLoto
        android:id="@+id/viewLoto5"
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" >
    </com.example.loto.ViewLoto>

    <com.example.loto.ViewLoto
        android:id="@+id/viewLoto6"
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" >
    </com.example.loto.ViewLoto>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <com.example.loto.ViewLoto
        android:id="@+id/viewLoto7"
        android:layout_width="102dp"
        android:layout_height="wrap_content" >

    </com.example.loto.ViewLoto>

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="24sp"
        android:text="The result will be here!" />

</LinearLayout>

<Button
    android:id="@+id/btnDraw"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Draw numbers" />

</LinearLayout>

和java代码:

package com.example.loto;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;


public class ActivityLoto extends Activity
{
   class Layout 
   {
       public Layout()
       {
           txtResult = (TextView)findViewById(R.id.txtResult);
           btnDraw = (Button)findViewById(R.id.btnDraw);
       }

       TextView txtResult;
       Button btnDraw;
   }

   int[] views = new int[7];

   class Events
   {
       public Events()
       {
            l.btnDraw.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                    int[] numbers = new int[7];
                    for(int i=0;i<numbers.length;i++)//I stuck here 
                    {
                         int curent =i+1;
                         ViewLoto viewLoto = (ViewLoto)findViewById(R.id.viewLoto+curent);
                         numbers[i] = viewLoto.getValue();
                     }
                }
            });
       }
   }

   Layout l;
   Events e;

   @Override
   protected void onCreate(Bundle savedInstanceState) 
   {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loto);

        l = new Layout();
        e = new Events();
   }

}

2 个答案:

答案 0 :(得分:1)

您可以像这样循环浏览特定View的子项:

List<Integer> lotoValues = new ArrayList<Integer>();
ViewGroup layout = (ViewGroup) findViewById(R.id.myLayout);
for(int i=0; i<layout.getChildCount(); ++i) {
    ViewLoto nextChild = (ViewLoto) layout.getChildAt(i);
    lotoValues.add((Integer)nextChild.getText());
}

编辑:

上面的代码将遍历布局的子项及其getText结果到数组lotoValues。请注意,您的自定义视图应该有一个方法getText来返回视图的文本。

<LinearLayout
    android:id="@+id/myLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <com.example.loto.ViewLoto
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <com.example.loto.ViewLoto
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <com.example.loto.ViewLoto
        android:layout_width="102dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

</LinearLayout>

答案 1 :(得分:0)

解决方案1:

如果您的目标是避免为每个视图创建侦听器,只需在您的类中实现OnClickListener,并在侦听器上解压缩哪个视图(在该方法中,您需要为每个视图声明id

解决方案2:

动态创建视图并将其添加到布局中:

View views = new View[7];
for(int i=0;i<views.length;i++){
    view[i] = new ViewLoto(); // your custom view
}

并通过调用yourLayout.addView():

将每个视图添加到您的布局中
LinearLayout yourLayout = (LinearLayout)findViewById(R.id.layout_id);
for(int i=0;i<views.length;i++){

   // here you can set attribute yo your view. for example onClickListener 
   // view[i].setOnClickListener(this);

    yourLayout.addView(views[i]);
}