當我運行我的代碼時,我沒有收到任何錯誤,但沒有任何反應我觸摸屏幕。全局變量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