gpt4 book ai didi

android - OnKeyListener 句柄按下两次

转载 作者:行者123 更新时间:2023-11-30 02:33:26 28 4
gpt4 key购买 nike

有导致警报对话框的 MainActivity,当打开的警报对话框不起作用时更改音量,放置 OnKeyListener,现在当您按下音量按钮时处理两次,MainActivity 中的监听器处理单击正确,帮助我理解

MainActivity 中的代码

    public void ShowTrack(final String smetaout,String stitleout){

if (MediaService.isPlayingDubstep || MediaService.isPlayingLiquid || MediaService.isPlayingMain == true) {
allbtnStop();
}
alert = new AlertDialog.Builder(this).create();
alert.setTitle(stitleout);
alert.setCanceledOnTouchOutside(false);

LayoutInflater li = getLayoutInflater();
View v = li.inflate(R.layout.track_view, null);
Log.d("My Webview", "ShowTrack");
final WebView wv = (WebView) v.findViewById(R.id.trackView);
wv.setVisibility(View.GONE);
playTrack = (Button) v.findViewById(R.id.playTrack);
progress = (ProgressBar) v.findViewById(R.id.progress);
playTrack.setVisibility(View.INVISIBLE);
closeTrack = (Button) v.findViewById(R.id.close);
colors_sel = mSettings.getString("colors", "1");
if (colors_sel.equals("0")) {
playTrack.setBackgroundResource(R.drawable.stop_button_red);

}
else if (colors_sel.equals("1")) {
playTrack.setBackgroundResource(R.drawable.stop_button_green);

}
else if (colors_sel.equals("2")) {
playTrack.setBackgroundResource(R.drawable.stop_button_blue);

}

progress.setVisibility(View.VISIBLE);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(smetaout);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(url);
Log.d("shouldOverrideUrlLoading", url);

MediaService.releaseMP();

return true;
}

@Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
Log.d("onPageFinished", "loadingFinished = true;");
}

if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
if (loadtrack == true){
myUri = Uri.parse(url);
context = getApplicationContext();
at = (ActionTask) new ActionTask().execute();




playTrack.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
colors_sel = mSettings.getString("colors", "1");
if (MediaService.isPlayingTrack) {
MediaService.pauseT();
if (colors_sel.equals("0")) {
playTrack.setBackgroundResource(R.drawable.play_button_red);
} else if (colors_sel.equals("1")) {
playTrack.setBackgroundResource(R.drawable.play_button_green);
} else if (colors_sel.equals("2")) {
playTrack.setBackgroundResource(R.drawable.play_button_blue);
}
}else{
visibleProgressTrack();
MediaService.startT();
if (colors_sel.equals("0")) {
playTrack.setBackgroundResource(R.drawable.stop_button_red);
} else if (colors_sel.equals("1")) {
playTrack.setBackgroundResource(R.drawable.stop_button_green);
} else if (colors_sel.equals("2")) {
playTrack.setBackgroundResource(R.drawable.stop_button_blue);
}
}

Log.i("ShowTrack","isPlayingTrack="+MediaService.isPlayingTrack);
}
});
}
loadtrack = !loadtrack;
} else{
redirect = false;
}
}
});




alert.setView(v);

closeTrack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaService.isPlayingTrack = false;
if (MediaService.mediaPlayer != null) {
if (at != null){
at.cancel(false);
}
MediaService.stopT();
MediaService.releaseMP();
}
alert.dismiss();
}
});
alert.setOnKeyListener(new DialogInterface.OnKeyListener() {

@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {
MediaService.isPlayingTrack = false;
if (MediaService.mediaPlayer != null) {
if (at != null){
at.cancel(false);
}
MediaService.stopT();
Log.i("Key","Back");
MediaService.releaseMP();

}
dialog.dismiss();
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
int index = seekbar.getProgress();
seekbar.setProgress(index + 1);
Log.i("onKey","seekbar="+index);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
int index = seekbar.getProgress();
seekbar.setProgress(index - 1);
Log.i("onKey","seekbar="+index);
return true;
}
return onKeyDown(keyCode, event);

}
});
alert.show();
}

public static void initBar(SeekBar bar, final int stream) {
bar.setMax(am.getStreamMaxVolume(stream));
bar.setProgress(am.getStreamVolume(stream));

bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress,
boolean fromUser) {
am.setStreamVolume(stream, progress, AudioManager.FLAG_PLAY_SOUND);
am.setStreamVolume(stream, progress, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}

public void onStartTrackingTouch(SeekBar bar) {
// no-op
}

public void onStopTrackingTouch(SeekBar bar) {
// no-op
}


});

}

public boolean onKeyDown(int keyCode, KeyEvent event) {
//ProgressBar seekbar = (SeekBar)findViewById(R.id.seekBar1);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
int index = seekbar.getProgress();
seekbar.setProgress(index + 1);
Log.i("onKeyDown","seekbar="+index);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
int index = seekbar.getProgress();
seekbar.setProgress(index - 1);
Log.i("onKeyDown","seekbar="+index);
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK){
if (exit==false)
Toast.makeText(this, R.string.exit, Toast.LENGTH_SHORT).show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
exit = false;
}
}, 3000);
if (exit) {
stopService();
unregisterReceiver(myReceiver);
CallReceiver.state = false;
MainActivity.this.finish();
}
exit = true;
return true;

}
return super.onKeyDown(keyCode, event);
}

最佳答案

onKeyListener 检测按键按下和按键向上。这 2 个事件在 onKeyListener 中触发,并导致事件触发两次。

仅检查按键按下事件...您似乎没有在 onKey 方法中检测到此类事件。在你的 onKey 方法中这样的东西应该防止双重触发

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_UP) {
// your logic now goes in here
}
.....
}

关于android - OnKeyListener 句柄按下两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26968353/

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