2016-07-22 78 views
1

我正在開發此應用程序,並正在對其進行測試,但我不太清楚哪裏出了什麼問題。當我點擊或按住Deck Button時,絕對沒有任何反應。只有在logcat中顯示的是Touch Events。我認爲這可能是我的格式有問題,但由於這是我第一次使用Java(過去只使用了微處理器類的程序集),我不確定它會是什麼。Android Studio:ImageButton在點擊時未激活

編輯:刪除了很多額外的代碼無關的每個建議

這裏的問題是MainActivity.java

public class MainActivity extends AppCompatActivity { 
    int i=0; 
    int VOA=0; 
    int VOB=0; 
    int [] c ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; 



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


// The Draw Control for the Deck. It will only draw if it is in it's 0 state 
     ImageButton Deck =(ImageButton)(findViewById(R.id.Deck)); 
     assert Deck != null; 
     Deck.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(VOA==0){ 
        VOA=c[i]; 
        i=i+1; 


       } else if (VOB==0){ 
        VOB=c[i]; 
        i=i+1; 

       } 

      } 
     }); 

// Uses Collections shuffle to shuffle c 
     Deck.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       List c= new ArrayList(); 
       Collections.shuffle(c); 


       return false; 
      }}); 


//Discard for Objective A by setting it to 0 and thus updating it's case statement. 
     ImageButton ObjectiveA =(ImageButton)(findViewById(R.id.ObjectiveA)); 
     assert ObjectiveA != null; 
     ObjectiveA.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       VOA=0; 
       return false; 
      } 
     }); 



//Discard for Objective B by setting it to 0 and thus updating it's case statement. 
     ImageButton ObjectiveB =(ImageButton) (findViewById(R.id.ObjectiveB)); 
     assert ObjectiveB != null; 
     ObjectiveB.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       VOB=0; 
       return false; 

      } 
     }); 

     } 
    } 
} 

這裏是activity_main.xml中以及

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="gallostsp.darkagedeck.MainActivity" 
    android:background="@drawable/background" 
    android:screenOrientation="landscape"> 



    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/Deck" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@+id/VerticalCenterLine" 
     android:layout_toStartOf="@+id/VerticalCenterLine" 
     android:background="@drawable/deck" 
     android:contentDescription="@string/objectives_deck" 
     android:clickable="true" 
     android:longClickable="true" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/ObjectiveA" 
     android:background="@drawable/objectivearea" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_toRightOf="@+id/VerticalCenterLine" 
     android:layout_toEndOf="@+id/VerticalCenterLine" 
     android:layout_above="@+id/ObjectiveB" 
     android:contentDescription="@string/objective_a" 
     android:longClickable="true" 
     android:clickable="false" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/ObjectiveB" 
     android:layout_below="@+id/HorizontalCenterLine" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:background="@drawable/objectivearea" 
     android:layout_toRightOf="@+id/VerticalCenterLine" 
     android:layout_toEndOf="@+id/VerticalCenterLine" 
     android:layout_alignParentBottom="true" 
     android:contentDescription="@string/objective_b" 
     android:longClickable="true" 
     android:clickable="false" /> 


</RelativeLayout> 
+1

建議閱讀:[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – wogsland

回答

2

如果您消耗longclickListener()

返回true 210
+0

感謝這有助於我可以告訴按鈕實際上工作,我將不得不用我的代碼做更多的測試來弄清楚它有什麼問題。現在謝謝你。 – Michael