2014-06-17 17 views
1

我有一个对话框菜单设置,供用户选择他们希望构建的建筑物。当他们按下购买按钮时,建筑物会自动插入到活动中。问题是,如果购买不止一个,那么移动最近购买的建筑物也将移动第二个最近购买的建筑物。我不积极如何解决这个问题。我会看到如果在框架布局中设置新建筑而不是线性布局会有所帮助,但是如果我这样做,则我的拖放操作不起作用。动态创建多个图像按钮使拖放变得困难

下面是ImageButton的代码被设置了购买按钮被按下后:

  layout = (LinearLayout) findViewById(R.id.newColonyHutLayout); 
      buyColonyHut = (Button) buildingSelect.findViewById(R.id.buyColonyHut); 
      buyColonyHut.setText("Buy"); 
      buyColonyHut.setBackgroundColor(-65536); 
      buyColonyHut.setOnClickListener(new OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        //get current amount of rock and check if it is enough to buy 
        data = new Database(runGraphics.this); 
        data.open(); 
        rockAmount = data.getRockAmount(); 
        data.close(); 
        if (rockAmount >= 100) 
        { 
         //take away spent money and update database 
         data.open(); 
         rockAmount -= 100; 
         data.rockAmountEntry(rockAmount); 
         data.close(); 

         //update the scores 
         updateScores(); 

         //close the dialog 
         buildingSelect.dismiss(); 
         buildMoreButton.setBackgroundColor(-65536); 

         //they have enough rock, build the building 
         newColonyHut = new ImageButton(runGraphics.this); 
         newColonyHut.setBackgroundResource(R.drawable.mainhut); 
         newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
         layout.addView(newColonyHut); 
         newColonyHut.setOnTouchListener(new OnTouchListener() 
         { 
          @Override 
          public boolean onTouch(View v, MotionEvent event) 
          { 
           if (event.getAction() == MotionEvent.ACTION_DOWN) 
           { 
            newColonyHutSelected = true; 
           }//end if 

           if (event.getAction() == MotionEvent.ACTION_UP) 
           { 
            if (newColonyHutSelected == true) 
            { 
             newColonyHutSelected = false; 
            }//end if 
           }//end if 
           else if (event.getAction() == MotionEvent.ACTION_MOVE) 
           { 
            if (newColonyHutSelected == true) 
            { 
             LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(v.getWidth(), v.getHeight()); 
             params.setMargins((int)(event.getRawX() - event.getRawY()/5), (int) (event.getRawY() - v.getHeight()), (int) (event.getRawX() - v.getWidth()/5), (int) (event.getRawY() - v.getHeight())); 
             v.setLayoutParams(params); 
            }//end if 
           }//end else 

           return false; 
          }//end onTouch function 

         });//end setOnTouchListener 
        }//end onClick function 
       });//end onClickListener 

这里是XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:id="@+id/newColonyHutLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <ImageButton 
      android:id="@+id/newColonyHut" 
      android:visibility="invisible" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mainhut" /> 
    </LinearLayout> 
</FrameLayout> 
+0

如果你想实现拖放,你可以使用android提供的拖放。它可以在API级别11和更高级别上运行。 http://developer.android.com/guide/topics/ui/drag-drop.html – berserk

+0

http://stackoverflow.com/questions/8570982/disable-or-prevent-multitouch-in-activity检查此链接,也许这将有助于避免多点触摸。 – berserk

回答

0

尝试在onTouch()结束返回true告知TouchEvent已被侦听器使用的方法。否则,被触摸的View下面的View的后续监听器将被调用 - 例如,你的第二座建筑之一。

另一个想法是使用View.onDragListener,而不是自行完成拖放操作。这里有一个很好的例子:http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html#draganddrop_overview

+0

如果他返回true,它将在ACTION_DOWN消耗事件并且不允许ACTION_MOVE。 – berserk

+0

好吧,View.OnDragListener怎么样,而不是完全自己实现拖放? – tknell

+0

这是一个很好的选择,易于实施,但需要API 11及以上版本。但是,这是一个替代方案。 – berserk