按照此处的说明:How to make custom seek bar in android?我能够制作自己的自定义搜索栏,在拇指内绘制文本。我知道要创建我自己的seekbar类,以便我可以从我的活动中调用它。我遇到了问题,因为当我在主要活动中创建一个seekbar时,它无法正常工作。我认为我没有正确设置OnSeek Listener。这是我的主类:如何创建单独的seekBar类
public class MainActivity extends Activity {
CustomSeekBar test;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
test = (CustomSeekBar) findViewById(R.id.seekBar1);
setContentView(R.layout.activity_main);
}
}
这里是我的CustomSeekBar类:
public class CustomSeekBar extends SeekBar implements OnSeekBarChangeListener {
String TAG = "TOUCHING";
SeekBar seekbar;
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int value = seekBar.getProgress();
String valueString = value + "";
seekBar.setThumb(writeOnDrawable(R.drawable.thumbler_small, valueString));
Log.d(TAG, "Thumb is being touched");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public BitmapDrawable writeOnDrawable(int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}
}
这里是我的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" >
<SeekBar
android:name="com.example.customseekbarclass"
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:indeterminate="false"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:progressDrawable="@drawable/styled_progress"
android:thumb="@drawable/thumbler_small" />
</RelativeLayout>
这是我的目录的安装方式: 如何在我的主要活动类中设置SeekBar,以便它从我的SeekBar类调用方法?
我已经编辑我的帖子给你看我的目录设置。我尝试使用上面的布局设置查找栏并使用 但我得到了一个膨胀异常。有任何想法吗? –
DMC
是的,我喜欢。你错过了CustomSeekBar的构造函数。你需要添加一个参数作为上下文和Attrs的参数。 NextTime发布您的logcat – Blackbelt
我已经完成了这一点,如我在上面编辑的代码所示。它现在运行,但它没有调用任何方法时,按下搜索栏! – DMC