任何人都可以处理它吗?
Can anyone deal with it?
我希望我的按钮(应该是按钮还是其他什么?)在我将鼠标悬停在它上面并停在 HOVER_EXIT 时播放音乐。我应该实施什么
case MotionEvent.ACTION_HOVER_MOVE:
让按钮继续播放音乐,从 ENTER
到 EXIT
和 MOVE
不做任何事情?
还有一个错误 - 当我尝试使用此悬停按钮打开屏幕时,应用程序崩溃并关闭。
这是我的 java 代码:
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class DisplayActivity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
b1 = (Button)findViewById(R.id.button1);
b1.setOnHoverListener(new View.OnHoverListener()
{
@Override
public boolean onHover(View v, MotionEvent event) {
MediaPlayer player=MediaPlayer.create(DisplayActivity.this,R.raw.sound);
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
player.start();
break;
case MotionEvent.ACTION_HOVER_EXIT:
player.stop();
break;
}
return true;
}
});}`
public class DisplayActivity extends AppCompatActivity {
private MediaPlayer player;
@Override
protected void onResume() {
super.onResume();
// create media player only when required
player = MediaPlayer.create(this, R.raw.sound);
// this will keep the audio playing, even if you hover for long time
player.setLooping(true);
}
@Override
protected void onPause() {
super.onPause();
// release is as soon as possible
player.release();
player = null;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
findViewById(R.id.button1).setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(
View v,
MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
player.start();
break;
case MotionEvent.ACTION_HOVER_EXIT:
// if you choose to stop the player, you need to prepare it again by calling player.prepare(); before restarting it.
// I chose to pause it and seek it back to beginning
player.pause();
player.seekTo(0);
break;
}
return true;
}
});
}
}
希望对您有所帮助:)
我希望我的按钮(应该是按钮还是其他什么?)在我将鼠标悬停在它上面并停在 HOVER_EXIT 时播放音乐。我应该实施什么
case MotionEvent.ACTION_HOVER_MOVE:
让按钮继续播放音乐,从 ENTER
到 EXIT
和 MOVE
不做任何事情?
还有一个错误 - 当我尝试使用此悬停按钮打开屏幕时,应用程序崩溃并关闭。
这是我的 java 代码:
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class DisplayActivity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
b1 = (Button)findViewById(R.id.button1);
b1.setOnHoverListener(new View.OnHoverListener()
{
@Override
public boolean onHover(View v, MotionEvent event) {
MediaPlayer player=MediaPlayer.create(DisplayActivity.this,R.raw.sound);
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
player.start();
break;
case MotionEvent.ACTION_HOVER_EXIT:
player.stop();
break;
}
return true;
}
});}`
public class DisplayActivity extends AppCompatActivity {
private MediaPlayer player;
@Override
protected void onResume() {
super.onResume();
// create media player only when required
player = MediaPlayer.create(this, R.raw.sound);
// this will keep the audio playing, even if you hover for long time
player.setLooping(true);
}
@Override
protected void onPause() {
super.onPause();
// release is as soon as possible
player.release();
player = null;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
findViewById(R.id.button1).setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(
View v,
MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
player.start();
break;
case MotionEvent.ACTION_HOVER_EXIT:
// if you choose to stop the player, you need to prepare it again by calling player.prepare(); before restarting it.
// I chose to pause it and seek it back to beginning
player.pause();
player.seekTo(0);
break;
}
return true;
}
});
}
}
希望对您有所帮助:)