gpt4 book ai didi

java - OnTouch 触摸时不运行

转载 作者:行者123 更新时间:2023-12-02 12:49:56 25 4
gpt4 key购买 nike

运行代码时没有收到任何错误,但触摸屏幕时没有任何反应。全局变量 select 的值应该改变,但没有任何反应。

这是代码

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);
}
}

编辑:这是全局类公共(public)类全局变量{

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

}

最佳答案

当你实现OntouchListener时,你需要在你的Context上设置监听器。

只需将其添加到构造函数中即可:应该是这样的:

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

关于java - OnTouch 触摸时不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44634772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com