2017-06-19 47 views
0

當我運行我的代碼時,我沒有收到任何錯誤,但沒有任何反應我觸摸屏幕。全局變量select的值應該改變,但沒有任何反應。當觸摸時OnTouch沒有運行

下面是代碼

public class NonmultiplierSixGame extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_nonmultiplier_six_game); 
    } 
} 

activity_nonmultiplier_six_game:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.alexandermain.example_5.NonmultiplierSixGame"> 





    <com.example.alexandermain.example_5.views.NonmultiplierSixView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/nonmultiplierSixView" 
     android:background="@color/colorPrimary" 
     /> 


</android.support.constraint.ConstraintLayout> 

NonmultiplierSixView類:

public class NonmultiplierSixView extends View implements View.OnTouchListener{ 



@Override 
protected void onDraw(Canvas canvas){ 
//bunch of shapes 
} 


@Override 
public boolean onTouch(View v, MotionEvent event) { 
    switch(event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      Globals.SetSelect(1); 
      break; 
     case MotionEvent.ACTION_UP: 
      Globals.SetSelect(2); 
      break; 

    } 

    return true; 
} 



public NonmultiplierSixView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 
} 

編輯: 這裏是Globals類 公共類全局{

public static int select=-2; 
public static void SetSelect(int t) { 
    select = t; 
} 
public static int GetSelect() { 
    return(select); 
} 

}

+0

提全局。這裏SetSelect功能 –

+0

與Globals類 –

+0

我編輯了OP – mathexplorer

回答

1

當你實現OntouchListener,你需要setThe您語境收聽。

只需在您的構造函數添加: 它應該是這樣的:

public NonmultiplierSixView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setOnTouchListener(this); 
} 
+0

謝謝!我沒有意識到我必須這樣做。但它仍然不起作用。還有其他建議嗎? – mathexplorer

+0

onDraw方法連續重複。因此,也許,即使你觸摸你的對象,由於onDraw,它每次都會重新繪製到它的初始位置。 所以你想要做的是例如設置一個布爾isPostionned,所以你的對象將被繪製在初始位置只有一次。 因此布爾值將最初爲false,然後在繪製項目後將其設置爲true。 例如:在您的NonmultiplierSixView類中聲明 boolean isPositionned = false; 比onDraw: if(!isPositionned){ //代碼定位你的對象 isPositionned = true; } –

+0

顯然,OnTouch正在與Globals合作,而不是。當我點擊它時,它會將日誌打印到日誌中。感謝您的幫助:) – mathexplorer

0

我是一個很沒有經驗的程序員,但我創建onTouch收聽今天早些時候的正常工作,

試圖改變onTouch方法是這樣的:

 public boolean onTouch(View v, MotionEvent event) { 

      switch (event.getAction()){ 
       case MotionEvent.ACTION_DOWN: 
        Globals.SetSelect(1); 
        return false; 
       case MotionEvent.ACTION_UP: 
        Globals.SetSelect(2); 
        return false; 
      } 
      return false; 
     } 

對不起提前如果它不爲你工作..

0

沒有必要實現View.OnTouchListenerView類已經有其onTouchEvent(MotionEvent event)方法。試試這個:

@Override 
public boolean onTouchEvent(MotionEvent event) { 
switch (event.getAction()){ 
       case MotionEvent.ACTION_DOWN: 
              break; 
       case MotionEvent.ACTION_MOVE: 
              break; 
       case MotionEvent.ACTION_UP: 
              break; 
      } 
      return true; 
     } 

它一定會努力。並檢查this也。

+0

謝謝你!我嘗試了這一點,雖然我沒有得到任何錯誤,但當我點擊屏幕時沒有任何反應。任何其他的想法可能是什麼問題? – mathexplorer

+0

想知道爲什麼它不工作,可能是因爲我們返回false(按照Android Docs「如果事件被處理返回True,否則返回false」)我已更新代碼檢查它 –

+0

您能解釋一下您在最後的評論。我對android studio非常陌生。我如何在每個條件中放入日誌並進行調試? – mathexplorer

0

onDraw方法不斷重複。因此,也許,即使你觸摸你的對象,由於onDraw,它每次都會重新繪製到它的初始位置。 所以你想要做的是例如設置一個布爾isPostionned,所以你的對象將被繪製在初始位置只有一次。 因此布爾值將最初爲false,然後在繪製項目後將其設置爲true。

例如:宣告在NonmultiplierSixView類

boolean isPositionned = false; 

比的onDraw:

if (!isPositionned){ 
    //code to position you object 
    isPositionned = true; 
    } 

(這是在情況下你的意圖是移動形狀)