当我运行我的代码时,我没有收到任何错误,但没有任何反应我触摸屏幕。全局变量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);
}
}
提全局。这里SetSelect功能 –
与Globals类 –
我编辑了OP – mathexplorer