2013-06-24 33 views
0

按照此处的说明: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> 

这是我的目录的安装方式: enter image description here 如何在我的主要活动类中设置SeekBar,以便它从我的SeekBar类调用方法?

回答

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" /> 
+0

我已经编辑我的帖子给你看我的目录设置。我尝试使用上面的布局设置查找栏并使用但我得到了一个膨胀异常。有任何想法吗? – DMC

+0

是的,我喜欢。你错过了CustomSeekBar的构造函数。你需要添加一个参数作为上下文和Attrs的参数。 NextTime发布您的logcat – Blackbelt

+0

我已经完成了这一点,如我在上面编辑的代码所示。它现在运行,但它没有调用任何方法时,按下搜索栏! – DMC

0

我解决了这个由ca从我的主要活动类灌装的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); 
    } 

} 
相关问题