1. 程式人生 > >Android-SeekBar可滑動進度條

Android-SeekBar可滑動進度條

目標效果:

   

程式執行顯示進度條在中間,手指滑動可更改進度條位置,並且顯示在TextView上,當點選進度條按鈕或者拖動時,按鈕變為紅色,擡起手指變為黑色。

1.activity_main.xml頁面設定控制元件。

activity_main.xml頁面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/seekbar"
        android:thumb="@drawable/thumb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50" />

    <TextView
        android:id="@+id/tvone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp" />

    <TextView
        android:id="@+id/tvtwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp" />

</RelativeLayout>
2.res資料夾下新建drawable資料夾,在裡邊新建thumb.xml頁面,用於編寫點選按鈕替換圖片的程式碼。 thumb.xml頁面:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/red" android:state_pressed="true"/><!-- 是否被按下 -->
    <item android:drawable="@drawable/red" android:state_focused="true"/><!-- 是否取得焦點 -->
    <item android:drawable="@drawable/red" android:state_selected="true"/><!-- 是否被選中 -->
    <item android:drawable="@drawable/black"/>

</selector>

3.MainActivity.java頁面編寫拖動事件。 MainActivity.java頁面:
package com.example.seekbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnSeekBarChangeListener{

	private SeekBar seekbar;
	private TextView tvone,tvtwo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		seekbar=(SeekBar) findViewById(R.id.seekbar);
		tvone=(TextView) findViewById(R.id.tvone);
		tvtwo=(TextView) findViewById(R.id.tvtwo);
		
		seekbar.setOnSeekBarChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
 		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/*數值改變*/
	@Override
	public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
		tvtwo.setText("當前數值:"+progress);
		tvone.setText("正在拖動");
	}

	/*開始拖動*/
	@Override
	public void onStartTrackingTouch(SeekBar seekbar) {
		tvone.setText("開始拖動");
	}

	/*停止拖動*/
	@Override
	public void onStopTrackingTouch(SeekBar arg0) {
		tvone.setText("停止拖動");
	}

}

4.執行就可以顯示目標效果了。