java - 无法与 OnClick\OnItemClickListener 中的视图及其事件交互
问题描述
package com.example.android.miwok;
//all the imports
public class VocabularyManager extends Application{
ProgressBar progress;
ImageButton volup;
ImageButton voldown;
TextView current_vol_text;
//[Cut-out Definitions of Variables]
ImageButton.OnClickListener changevol;
public VocabularyManager(Activity activity, Context context, String Category) {
//Don't wonder, many things up here are also cut-out
dialogview = inflater.inflate(R.layout.player_dialog, null);
volup = dialogview.findViewById(R.id.vol_up); //MARKER #1
voldown = dialogview.findViewById(R.id.vol_down); //MARKER #2
progress = dialogview.findViewById(R.id.progress); //MARKER #3
list = (ListView) this.act.findViewById(R.id.vocabulary_list);
listadapter = new WordAdapter(this.act, mwords);
list.setBackgroundColor(context.getResources().getColor(R.color.category_family));
list.setAdapter(listadapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
list.setEnabled(false);
Word word = mwords.get(i);
timer = new Timer();
dialog = new AlertDialog.Builder(mContext);
dialogview = inflater.inflate(R.layout.player_dialog, null);
volup.setOnClickListener(changevol);
voldown.setOnClickListener(changevol);
progress = dialogview.findViewById(R.id.progress);
current_vol_text = dialogview.findViewById(R.id.current_stream_volume);
current_vol_text.setText(Integer.toString(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)));
//Setting up the 'dialog' AlertDialog.Builder
crdialog = dialog.create();
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
if(mMediaPlayer == null) {
mMediaPlayer = MediaPlayer.create(mContext, word.getAudio());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
progress.setMax(mMediaPlayer.getDuration());
progress.setProgress(0);
crdialog.show();
ttask = new TimerTask() {
@Override
public void run() {
if(mMediaPlayer != null) {
progress.setProgress(mMediaPlayer.getCurrentPosition());
}
}
};
timer.scheduleAtFixedRate(ttask, 0, 50);
}
});
changevol = new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view.getId() == R.id.vol_up){
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,0);
}
else{
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,0);
}
current_vol_text.setText(Integer.toString(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)));
}
};
}
}
这段代码的问题是:我试图STREAM_MUSIC
在 OnClickListener( volchange
) 中为两个按钮volup
和voldown
. 出于某种原因,OnClickListener 没有触发,但如果我打开 logcat,则不会显示任何错误、警告或可疑日志。是什么导致了这种行为?
正如在评论中提到的,代码中不必要的部分被删掉了。
解决方案
我认为您需要从 ImageButton 中删除“最终”。
推荐阅读
- openlayers-3 - api3.geo.admin.ch v 3.6.0 OpenLayers 3 DragBox 在使用 firefox 时不显示在地图上(在 chrome 和 IE edge 上运行良好)
- android - 我想设置字段值以使用 Volley RequestQueue 但不起作用
- flutter - Flutter、NestedScrollView 和包裹体效果
- json - 合并从 Json 文档生成的两个 JSON Schemata
- python - 在 Micropython 中以文本分隔符格式保存文件
- php - 在实体中创建子对象而不需要额外的 SQL 表
- c# - 如何使用实体框架正确填充表
- reactjs - React,如何测试关键道具?
- android-studio - 升级到 Android Studio 4.1.3 后,Android 模拟器几乎每周需要擦除一次数据
- django-models - 很难弄清楚如何建立“服务”模型