2017-06-19 80 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; 
    } 

(这是在情况下你的意图是移动形状)