我有这段代码,我想在各种 TextView 中设置表示当前音量级别的文本。示例:系统容量:35%。显然,当我“移动” SeekBar 时,这个数字必须改变。这是代码。
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class Activity_JellyBean extends DashBoardActivity
{
/** Called when the activity is first created. */
private SeekBar volumeSeekbar = null;
private SeekBar alarmSeekbar = null;
private SeekBar systemSeekbar = null;
private SeekBar notificationSeekbar = null;
private SeekBar ringSeekbar = null;
private AudioManager audioManager = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_jellybean);
initControls();
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
alarmSeekbar = (SeekBar)findViewById(R.id.seekBar2);
systemSeekbar = (SeekBar)findViewById(R.id.seekBar3);
notificationSeekbar = (SeekBar)findViewById(R.id.seekBar4);
ringSeekbar = (SeekBar)findViewById(R.id.seekBar5);
final TextView media = (TextView)findViewById(R.id.textView1);
TextView ring = (TextView)findViewById(R.id.textView2);
TextView alarm = (TextView)findViewById(R.id.textView3);
TextView noti = (TextView)findViewById(R.id.textView4);
TextView ringe = (TextView)findViewById(R.id.textView5);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
alarmSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_ALARM));
alarmSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_ALARM));
systemSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
systemSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_SYSTEM));
notificationSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
notificationSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_NOTIFICATION));
ringSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_RING));
ringSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_RING));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
alarmSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,
progress, 0);
}
});
systemSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,
progress, 0);
}
});
notificationSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
progress, 0);
}
});
ringSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_RING,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
提前致谢。