android seekbar的用法

package com.example.test.sliderbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.util.Log;

public class MainActivity extends AppCompatActivity  implements SeekBar.OnSeekBarChangeListener{
    TextView seekBarResult;
    SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar=(SeekBar) findViewById(R.id.seekBar);
        seekBarResult=(TextView) findViewById(R.id.textView);
        seekBar.setOnSeekBarChangeListener(this);
    }

    @Override//这两个函数必须重载,尽管可以没有具体内容
    public void onStartTrackingTouch(SeekBar seekBar) {

    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
                                  boolean fromUser) {
        Log.d("SeekBar", "seekid:"+seekBar.getId()+", progess"+progress);
        switch(seekBar.getId()) {
            case R.id.seekBar:{
                // 设置“与系统默认SeekBar对应的TextView”的值
                seekBarResult.setText("SeekBar Value : "+String.valueOf(seekBar.getProgress()));
                break;
            }
            default:
                break;
        }
    }
}


发布了158 篇原创文章 · 获赞 181 · 访问量 64万+
展开阅读全文

android关于自定义seekbar控件的问题(将横向seekbar改成竖向seekbar

12-10

自定义了个seekbar,将横向的seekbar改为了竖向的seekbar,目前只支持触摸,但是用遥控器控制时候按上下按钮没响应,按下左右按钮seekbar才会被拖动 追了下代码seekbar继承自AbsSeekbar,AbsSeekBar继承自progressBar,ProgressBar继承自View,我该如何实现将按下左右键seekbar的progress值发生改变改为按下上下键seekbar的progress值发生改变?以下是我写的自定义seekbar的代码: import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.SeekBar; public class VerticalSeekBarTwo extends SeekBar { public VerticalSeekBarTwo(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public VerticalSeekBarTwo(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public VerticalSeekBarTwo(Context context) { super(context); // TODO Auto-generated constructor stub } protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(h, w, oldh, oldw); } @Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(heightMeasureSpec, widthMeasureSpec); setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); } protected void onDraw(Canvas c) { c.rotate(-90); c.translate(-getHeight(), 0); super.onDraw(c); } @Override public synchronized void setProgress(int progress) { super.setProgress(progress); onSizeChanged(getWidth(), getHeight(), 0, 0); } @Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: setProgress(getMax() - (int) (getMax() * event.getY() / getHeight())); onSizeChanged(getWidth(), getHeight(), 0, 0); break; case MotionEvent.ACTION_CANCEL: break; } return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (isEnabled()) { int progress = getProgress(); switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: if (progress <= 0) break; setProgress(progress - 1); return true; case KeyEvent.KEYCODE_DPAD_DOWN: if (progress >= getMax()) break; setProgress(progress + 1); return true; case KeyEvent.KEYCODE_DPAD_LEFT: break; case KeyEvent.KEYCODE_DPAD_RIGHT: break; } } return super.onKeyDown(keyCode, event); } } ``` ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览