Android 之 SeekBar用法介绍

拖动条:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/font"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:ems="10"
        android:lines="5"
        android:text="看我大小,别翻眼..."
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>
    
    <SeekBar 
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:scrollbarStyle="insideOverlay"
        android:progress="1"
        />

</LinearLayout>

主程序入口:

package com.example.advancedview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * SeekBar 功能演示, 拖动来改变字体的大小
 * @author Administrator
 *
 */
public class SeekBarActivity extends Activity {

	//声明变量
	private SeekBar seekBar;
	private EditText font;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.seek_bar_layout);
		
		font = (EditText) findViewById(R.id.font);
		seekBar = (SeekBar) findViewById(R.id.seekBar);
		
		seekBar.setOnSeekBarChangeListener(seekListerner);
		
	}
	
	// 拖动条事件
	private OnSeekBarChangeListener seekListerner = new OnSeekBarChangeListener() {
		
		// 拖动条停止执行
		public void onStopTrackingTouch(SeekBar seekBar) {
			
			Toast.makeText(SeekBarActivity.this, "停止了:"+seekBar.getProgress(), 1000).show();
		}
		
		// 开始执行
		public void onStartTrackingTouch(SeekBar seekBar) {
			
			Toast.makeText(SeekBarActivity.this, "开始了:"+seekBar.getProgress(), 1000).show();
		}
		
		// 拖动中
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			
			font.setTextSize(progress); // 改变字体的大小
		}
	};
}

相关推荐