如何在 int 数组中获取 R.id? [英] how to get R.id's in the int array?

查看:28
本文介绍了如何在 int 数组中获取 R.id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ObjectsClass 中获取我在 level1.xml 中的图像视图的 id 并且我在 GamePlayActivity 中夸大了这个布局...我的 ObjectClass 不是活动那么如何获取该类中的 id 数组...这是我的 level.xml 说有 15 个 ImageView 我刚刚展示了几个......

I want to get the id's of my imageviews in my ObjectsClass which are in level1.xml and I have inflated this layout in GamePlayActivity...MY ObjectClass is not activity then how to get array of id's in that class ...here is my level.xml say there are 15 ImageView I have just shown few ....

     <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/gmw_01"
 android:onClick="onClick"
 android:layout_height="wrap_content" 
android:layout_width="wrap_content"
 android:id="@+id/relativeLayout1" >
 <ImageView 
android:onClick="objectClick" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/imageView1"
 android:src="@drawable/bb01" 
android:layout_marginLeft="998dp" 
android:layout_marginTop="593dp" 
android:layout_alignParentTop="true"
 android:layout_alignParentLeft="true"/>
 <ImageView 
android:onClick="objectClick"
 android:layout_height="wrap_content" 
android:layout_width="wrap_content"
 android:id="@+id/imageView2"
 android:src="@drawable/bb02"
 android:layout_marginLeft="20dp" 
android:layout_marginTop="39dp" 
android:layout_alignParentTop="true"
 android:layout_alignParentLeft="true"/>
 <ImageView
 android:onClick="objectClick"
 android:layout_height="wrap_content" 
android:layout_width="wrap_content"
 android:id="@+id/imageView3"
 android:src="@drawable/bb03" 
android:layout_marginLeft="497dp" 
android:layout_marginTop="153dp"
 android:layout_alignParentTop="true" 
android:layout_alignParentLeft="true"/>

这是我的 GameplayActivity

and here is my GameplayActivity

 public class GamePlayActivity extends Activity {

    static int ObjectsFound;


     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gameplay);


            // ViewGroup where n number of view is going to added
            ViewGroup layout= (ViewGroup) findViewById(R.id.GamePlayScreen);

             // inflating the layout depending on the level 
            View level = View.inflate(this, LevelSelectionActivity.levelscreen, null);

            ** getRandom();    // here I want to call that random because this should be done     before my level is added on the view**

    // adding level bg for the respective selected level
            layout.addView(level);
}   

 public void objectClick(View objectClicked)
     {
         Toast msg;
         int Object = objectClicked.getId();
         ImageView img= (ImageView)findViewById(objectClicked.getId());
        switch (Object) {
        case R.id.imageView1:   
             img.setVisibility(View.INVISIBLE);
             msg = Toast.makeText(GamePlayActivity.this, "Bubble Found", Toast.LENGTH_SHORT);
             msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
             msg.show();

            break;

        }
}

和对象类

    public class Objects {


     int ObectId[];

     Objects(Context context)
     {
         super();
         for(int i=0;i<15;++i)
         {
             **ObectId[i]=R.id.;**     // what to get it over here  ? ? ? 
         }

     }

     public void randomize() {
   Random generator = new Random();
   for(int i = 0; i<8 ; i++) {
       while(true) {
           **View v = findViewById(generator.nextInt(Objectid.length));**
           if(!v.isClickable()) {
                v.setClickable(false);
                break;
           }
       }
   }
}


}

最后,我希望随机数量的对象不可点击,即每次用户只能在屏幕上点击 8 个对象时,有 15 个对象,所以在我的关卡开始之前,即在我在游戏活动中膨胀之前,如上所示我想得到随机 8 个可点击,反之亦然...

at the end I want random number of objects to be unclickable i.e. out of 15 objects everytime user can have only 8 objects clickable on the screen so before my level starts i.e. before I inflate in my gameplayActivity as seen above I want to get random 8 clickable and vice versa...

添加到它应该适用于所有级别......现在我有一个 level1.xml 如何实现所有级别......我是否需要在所有布局中提供相同的图像视图ID?

Adding to that it should work for all level ...right now I have one level1.xml how to achieve it for all levels... do i need to give same id's of image view in all layout ?

推荐答案

除了您感兴趣的每个 ImageView 之外,在 RelativeLayout 中是否还有任何视图?如果没有,只需使用布局上的 getChildCount()getChildAt() 方法来获取您需要的视图.任何 ViewGroup 已经将其子项存储在一个数组中,因此如果这些是其中包含的唯一视图,则没有理由重复该行为:

Are there any views inside that RelativeLayout besides each ImageView you're interested in? If not, just use the getChildCount() and getChildAt() methods on the layout to obtain the views you need. Any ViewGroup already stores its children in an array, so there's no reason to duplicate the behavior if those are the only views contained there:

//Somewhere in your code get a reference to the layout
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayout1);

//Then, update your view selection line like so
**View v = layout.getChildAt(generator.nextInt(layout.getChildCount()));**

HTH

这篇关于如何在 int 数组中获取 R.id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆