我正在开发一个应用程序,我希望在可扩展的标题视图中出现一个媒体按钮,即按钮将执行播放/停止功能。 如何在播放歌曲时更改按钮的背景以停止背景,并在停止时将其更改为播放背景。 当我想要在可扩展列表视图中的下一首歌曲出现时,我应该停止播放一首歌并将其背景更改为播放时,我也遇到了问题。
我的源代码是:::
在Expandable listview中,我已经完成了
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.my_list_row, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.medicineName);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
final ImageView playpause = (ImageView) convertView
.findViewById(R.id.actionPlaypause);
playpause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
///custom trick
v.setTag(orignalMedName);
mybtnListen.ListenToBtn(headerTitle, v);
}
});
if (groupPosition % 2 == 0) {
convertView.setBackgroundColor(Color.parseColor("#BBEBFF"));
} else {
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
}
return convertView;
}
在我的活动中实施此视图
case R.id.actionPlaypause:
final ImageView actionPlPause = (ImageView) bt
.findViewById(R.id.actionPlaypause);
// previmg.add(actionPlPause);
Log.d("currentid", "" + actionPlPause.getId());
Uri myUri;
DatabaseHandler dbcheck = new DatabaseHandler(getActivity());
Log.d("mediacheck", "" + "media is playing");
// actionPlPause.setImageResource(R.drawable.stop);
boolean recordCheck = dbcheck.checkSinlgeEntry(
DatabaseHandler.TABLE_MEDICINERECORD, medicineName);
if (recordCheck) {
myUri = Uri.parse(Environment.getExternalStorageDirectory()
+ "/SaySong/USaRecord/" + medicineName
+ ".mp3"); // initialize Uri here
} else {
myUri = Uri.parse(Environment.getExternalStorageDirectory()
+ "/SaySong/USaRecord/"
+ medicineName + ".mp3"); // initialize Uri here
}
// mediaPlayer = new MediaPlayer();
if (!mediaPlayer.isPlaying()) {
// mediaPlayer.start();
actionPlPause.setImageResource(R.drawable.stop);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getActivity(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
SaveInRecentStore(medicineName);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Log.i("Completion Listener", "Song Complete");
Log.d("mediacheck", "" + "media playing finished");
actionPlPause.setImageResource(R.drawable.play);
mp.stop();
mp.reset();
// mp.release();
}
});
} else {
mediaPlayer.pause();
mediaPlayer.stop();
mediaPlayer.reset();
actionPlPause.setImageResource(R.drawable.play);
}
break;
答案 0 :(得分:0)
只需设置按钮背景onClick:
playpause.setBackgroundRessource(R.drawable.yourBackground)
多数民众赞成