2017-11-25 110 views
0

我想让“a”变量取得seekbar的值并将其用于“睡眠”参数。现在,'a'是100.这可以用int来完成吗?我想要的:a = SeekBar的值为int值而不是100.我尝试过不同的方法,但没有任何运气。我也附加了xml代码。如何使用Seekbar值作为int变量?

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> 

回答

0

试试这个

​​3210