Android入门之SeekBar(纯java)


 Android入门之SeekBar(纯java)_第1张图片     Android入门之SeekBar(纯java)_第2张图片

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarTest extends Activity {
   
	private LinearLayout mainLayout=null;//主容器
	private TextView tv=null;//文本
	private SeekBar sb=null;//可拖动进度条
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainLayout_init();
        setContentView(mainLayout);
    }
    
    /*mainLayout主容器初始化*/
    void mainLayout_init(){
    	mainLayout=new LinearLayout(this);
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-1);
        mainLayout.setLayoutParams(lp);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        tv_init();
        mainLayout.addView(tv);
        sb_init();
        mainLayout.addView(sb);
    }
    /*tv文本初始化*/
    void tv_init(){
    	tv=new TextView(this);
    	tv.setText("这里显示进度\n");
    	tv.append("当前选中进度:50%");
    }
    /*sb可拖动进度条初始化*/
    void sb_init(){
    	sb=new SeekBar(this);
    	//设置进度条长度为300
    	LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(300, -2);
    	sb.setLayoutParams(lp);
    	//设定初始进度为50%
    	sb.setProgress(50);
    	//绑定监听
    	OnSeekBarChangeListener osbcl=new OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
				tv.setText("这里显示进度\n");
				tv.append("当前选中进度:"+progress+"%");
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}
    	};
    	sb.setOnSeekBarChangeListener(osbcl);
    }
}

你可能感兴趣的:(java,android,Class,import)