Android SeekBar扩展

时间:2012-12-17 15:39:53

标签: java android seekbar

我的活动中有多个Seek Bars(4),每个都有一个textView,它假设显示每个SeekBar的进度。我想扩展SeekBar类并向它的构造函数添加一个TextView参数,这样我就可以绑定两个 - > SeekBar和TextView。这是我的扩展SeekBar类:

import android.content.Context;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekBarPlus extends SeekBar {
    private TextView numberOfDrills;

    public SeekBarPlus(Context context, TextView text) {
        super(context);
        // TODO Auto-generated constructor stub
        numberOfDrills = (TextView) text;   
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        numberOfDrills.setText(progress); 
    }

} 

什么是上下文选项?

现在这是我的MainActivity:

package com.simplemathgame;

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;
import com.simplemathgame.SeekBarPlus;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
        TextView numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
        TextView numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
        TextView numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);

                SeekBarPlus addSeekBar = SeekBarPlus((SeekBar) findViewById(R.id.add_seek_bar), numberOfAddDrills);
    SeekBarPlus subSeekBar = SeekBarPlus((SeekBar) findViewById(R.id.sub_seek_bar), numberOfAddDrills);
    SeekBarPlus mulSeekBar1 = SeekBarPlus((SeekBar) findViewById(R.id.mul_seek_bar), numberOfAddDrills);
    SeekBarPlus divSeekBar1 = SeekBarPlus((SeekBar) findViewById(R.id.div_seek_bar), numberOfAddDrills);

    }   
}

我不知道如何怀疑!任何建议将不胜感激!!!

1 个答案:

答案 0 :(得分:0)

尝试在自定义SeekBar中使用setter而不是构造函数:

public void setTextView(TextView text) {
    numberOfDrills = text;   
}

然后在您的活动中,使用

SeekBarPlus subSeekBar = (SeekBarPlus) findViewById(R.id.sub_seek_bar);
subSeekBar.setTextView(numberOfAddDrills);

你也可以使用普通的Seekbar和OnSeekBarChangeListener:

SeekBar subSeekBar = (SeekBar) findViewById(R.id.sub_seek_bar);
subSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                numberOfDrills.setText(String.valueOf(progress)); 
            }
        });