如何将Seekbar值用作int变量?

时间:2017-11-25 15:21:30

标签: java android xml

我想创建“a”变量来获取搜索栏的值并将其用于“sleep”参数。现在,'a'是100.这可以用int完成吗?我想要的是:a = SeekBar的值为int值而不是100.我尝试了不同的方法,但没有任何运气。我还附加了xml代码。

public class Blink1 extends AppCompatActivity {

Handler handler ;
ConstraintLayout blink1 ;
int i = 0;
int colors[] = {Color.BLACK, Color.WHITE};
Runnable runnable = new Runnable() {
    @Override


        public void run () {
        blink1.setBackgroundColor(colors[i]);
        if(i==1){
            i=0;
        }
        else i++;

    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_blink1);

    handler = new Handler();
    blink1 = (ConstraintLayout) findViewById(R.id.blink2);

    final int a;
    a=100;

    Thread myTread = new Thread() {
            public void run() {
                try {
                    while (true) {
                        sleep(a);
                        handler.post(runnable);

                    }

                } catch (InterruptedException e) {
                }
            }
        };
        myTread.start();
    }
    }

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout       xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/blink2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.studios.minimal.blink.Blink1">

<SeekBar
    android:id="@+id/seekBar7"
    android:max="1000"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.903" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

试试这个

SeekBar seekBar=findViewById(R.id.seekBar7);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                a=seekBar.getProgress();
            }
        });