按照此处的说明操作:How to make custom seek bar in android?我能够创建自己的自定义搜索栏,在拇指内部绘制文字。我知道想创建自己的搜索栏类,以便我可以从我的活动中调用它。我遇到了问题,因为当我在主要活动中创建一个搜索栏时,它无法正常工作。我认为我没有正确设置OnSeek Listener。这是我的主要课程:
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类调用方法?
答案 0 :(得分:0)
你应该切换
test = (CustomSeekBar) findViewById(R.id.seekBar1);
setContentView(R.layout.activity_main);
因为R.id.seekBar1
属于activity_main.xml
,否则您将获得null
进行测试:
setContentView(R.layout.activity_main);
test = (CustomSeekBar) findViewById(R.id.seekBar1);
然后我会以这种方式更改布局的搜索栏部分
<com.example.CustomSeekBar
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" />
答案 1 :(得分:0)
我通过从我的主要活动类调用OnSeekBarChangeListener来解决这个问题:
public class MainActivity extends Activity {
CustomSeekBar test;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (CustomSeekBar) findViewById(R.id.seekBar1);
test.setOnSeekBarChangeListener(test);
}
}
并从我的CustomSeekBar类添加缺少的构造函数:
public class CustomSeekBar extends SeekBar implements OnSeekBarChangeListener {
String TAG = "TOUCHING";
SeekBar mSeekBar;
public CustomSeekBar(Context context) {
super(context);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
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
Log.d(TAG, "START");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.d(TAG, "STOP");
}
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);
}
}