0

我的活动中有多个 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);

    }   
}

我不知道怎么弄!任何建议,将不胜感激!!!

4

1 回答 1

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)); 
            }
        });
于 2012-12-17T15:44:47.193 回答