0

按照此处的说明操作:如何在 android 中制作自定义搜索栏?我能够制作自己的自定义搜索栏,在拇指内绘制文本。我知道想创建自己的搜索栏类,以便我可以从我的活动中调用它。我遇到了问题,因为当我在主要活动中创建搜索栏时,它无法正常工作。我认为我没有正确设置 OnSeek 侦听器。这是我的主要课程:

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 类中的方法?

4

2 回答 2

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" />
于 2013-06-24T10:17:25.520 回答
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);
    }

}
于 2013-06-24T11:44:01.600 回答