我有一个活动,用户可以看到有关我要开发的简单应用程序的“关于”。我正在使用“ Android About Page”来显示它,但是我想检测屏幕上的点击次数,以便在检测到正确的点击次数时出现Toast消息...某种复活节彩蛋: ) 我面临的问题是触摸事件没有触发...这是我正在使用的代码:
public class About extends AppCompatActivity {
@Override
public void onTouchEvent(MotionEvent event) {
Log.i("LOG_RESPONSE", "Screen touched!");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//// other code
}
}
这是我的XML:
<?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:id="@+id/layer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".About">
<View
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
没有错误,它什么都不做。
根据onTouch()的建议进行编辑:
public class About extends AppCompatActivity {
View test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test = (View)findViewById(R.id.myView);
test.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("LOG_RESPONSE", "Screen touched!");
return false;
}
});
//// other code
}
}
这样,应用程序崩溃:
Caused by: java.lang.NullPointerException
编辑2-它不再崩溃,但无法识别触摸:
public class About extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
View test = findViewById(R.id.layer);
Log.i("LOG_RESPONSE", test.toString());
test.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("LOG_RESPONSE", "Screen touched!");
return false;
}
});
}
答案 0 :(得分:1)
请参阅以下答案
https://stackoverflow.com/a/5002073/9640177
View用户使用onTouch()获取触摸事件,而View派生类使用onTouchEvent()获取触摸事件。
要实现您的目的,一种可能的解决方案是使用一个充满整个屏幕的透明视图,并在该视图上定义onTouchListner。
将以下视图添加为rootView的最后一个子视图
Produce a list of films which have a date of release before August 2016.
在您的onCreate()方法中,如下设置onTouchListener
<View android:id = "@+id/countTouch"
android:layout_height = "match_parent"
android:layout_weight = "match_parent"
android:background = "#00000000">
答案 1 :(得分:0)
由于您要计算整个屏幕上的每次点击,而不仅仅是特定视图,因此您应该在活动布局上设置一个触摸事件监听器,在您的情况下,它称为“ @ + id / layer”
这就是您想要的:
findViewById(R.id.layer).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
counter++;
if(counter==8)
Toast.makeText(About.this, "Surprise!!", Toast.LENGTH_SHORT).show();
Log.i("LOG_RESPONSE", "Screen touched!");
return false;
}
});
在示例中,我使用8作为事件的目标编号,但是您当然可以在上面声明并使用它。
编辑: 就 java.lang.NullPointerException 而言,原因是您将该视图声明为类成员对象,问题是您无法实例化该视图,因为它以上下文作为参数,并且onCreate方法尚未运行,好吧,没有上下文,因此您无法实例化它,因此最好在on create方法中对其进行声明。
那么,这就是你的问题:
public class About extends AppCompatActivity {
View test;//remove this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
test =(View)findViewById(R.id.myView);
//// other code
}
}
这就是解决方法
public class About extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
View test =(View)findViewById(R.id.myView);//Declare it and initialize it here
//// other code
}
}
答案 2 :(得分:0)
这是对最新问题的解答-是对上一部分答案实施后剩下的问题的回应
您需要将wrap_content
更改为match_parent
。视图中没有任何内容,因此wrap_content
会将其高度和宽度设置为0。因此,当您触摸屏幕时,您将触摸父ConstraintLayout而不是您的View。
<View
android:id="@+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
希望这可以帮助到这里结束的人。