編輯:關於Android編程
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); } }
3.圖片資源自定義SeekBar+手機音量調節
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效果圖,更改後系統音量值
一、概述在Android的開發中,經常聽到“內存洩漏”這個詞。“內存洩漏”就是一個對象已經不需要再使用了,但是因為其它的對象持有該對象的引用,導致它的內存不能被回收。“內
前面有文章曾經地介紹過MediaPlayer的基本用法,這裡就更加深入地講解MediaPlayer的在線播放功能。本文主要實現MediaPlayer在線播放音頻的功能,由
上一篇文章主要講述了Android的TouchEvent的分發過程,其中有兩個重要的函數:onInterceptTouchEvent和onTouchEvent,這兩個函數
最近研究怎麼讓自己的程序節省幾行代碼。仔細想一想,我們在做客戶端類的APP時,最基礎,大量重復的場景就是:1.從服務器請求數據2.解析得到的數據並處理加以封裝3.將封裝好