package com.example.seekbarui;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//隐藏Actionbar后将toolbar设置上去替换Actionbar
setSupportActionBar(toolbar);
//初始化seekbar,TextView
seekBar = (SeekBar) findViewById(R.id.seekBar);
textView = (TextView) findViewById(R.id.result);
//seekbar设置监听
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/*
* seekbar改变时的事件监听处理
* */
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("当前进度:"+progress+"%");
Log.d("debug",String.valueOf(seekBar.getId()));
}
/*
* 按住seekbar时的事件监听处理
* */
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this,"按住seekbar",Toast.LENGTH_SHORT).show();
}
/*
* 放开seekbar时的时间监听处理
* */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this,"放开seekbar",Toast.LENGTH_SHORT).show();
}
});
//浮动button实例化
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//设置点击监听
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
//创建toolbar菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
//toolbar菜单选择监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(MainActivity.this,item.getTitle(),Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
-
-
-
package com.example.seekbarui;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
private SeekBar seekBar;
private TextView textView;
private AudioManager audioManager;
private int currentVolume,maxVolume;
private int volume=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//初始化seekbar,TextView
seekBar = (SeekBar) findViewById(R.id.seekBar2);
//初始化AudioManager
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//获取系统最大音量值
maxVolume =audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
//将seekBar的最大进度值设置成系统最大音量
seekBar.setMax(maxVolume);
//获取当前音量值
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
//设置当前值为进度条当前值
seekBar.setProgress(currentVolume);
//初始化Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
//隐藏Actionbar后将toolbar设置上去替换Actionbar
setSupportActionBar(toolbar);
//seekbar设置监听
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/*
* seekbar改变时的事件监听处理
* */
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//设置拖动的进度值为系统音量
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,progress,0);
//获取当前音量
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
//设置当前音量
seekBar.setProgress(currentVolume);
/* //获取屏幕属性
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
//设置屏幕亮度
layoutParams.screenBrightness =((float)seekBar.getProgress())/100;
Log.d("debug",layoutParams.screenBrightness+"");
Log.d("deb",seekBar.getProgress()+"");
//更新窗口属性
getWindow().setAttributes(layoutParams);*/
}
/*
* 按住seekbar时的事件监听处理
* */
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(SecondActivity.this,"按住seekbar",Toast.LENGTH_SHORT).show();
}
/*
* 放开seekbar时的时间监听处理
* */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(SecondActivity.this,"放开seekbar",Toast.LENGTH_SHORT).show();
}
});
}
//创建toolbar菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
//toolbar菜单选择监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(SecondActivity.this,item.getTitle(),Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
6.效果截图
图片分别为更改前系统音量值,Seekbar效果图,更改后系统音量值