2012-12-17 97 views
0

我在我的activity(4)上有多個Seek Bars,每個SeeView都有一個假設顯示每個SeekBar進度的textView。我想擴展SeekBar類,並添加到它的構造函數一個TextView參數,所以我可以綁定兩個 - > SeekBar和TextView。這裏是我擴展的SeekBar類:Android SeekBar擴展

import android.content.Context; 
import android.widget.SeekBar; 
import android.widget.TextView; 

public class SeekBarPlus extends SeekBar { 
    private TextView numberOfDrills; 

    public SeekBarPlus(Context context, TextView text) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     numberOfDrills = (TextView) text; 
    } 

    public void onStartTrackingTouch(SeekBar seekBar) { 
     // TODO Auto-generated method stub 

    } 

    public void onStopTrackingTouch(SeekBar seekBar) { 
     // TODO Auto-generated method stub 

    } 

    public void onProgressChanged(SeekBar seekBar, int progress, 
      boolean fromUser) { 
     // TODO Auto-generated method stub 
     numberOfDrills.setText(progress); 
    } 

} 

什麼是上下文選項?

現在,這裏是我的MainActivity:

package com.simplemathgame; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.TextView; 
import com.simplemathgame.SeekBarPlus; 

public class MainActivity extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number); 
     TextView numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number); 
     TextView numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number); 
     TextView numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number); 

       SeekBarPlus addSeekBar = SeekBarPlus((SeekBar) findViewById(R.id.add_seek_bar), numberOfAddDrills); 
    SeekBarPlus subSeekBar = SeekBarPlus((SeekBar) findViewById(R.id.sub_seek_bar), numberOfAddDrills); 
    SeekBarPlus mulSeekBar1 = SeekBarPlus((SeekBar) findViewById(R.id.mul_seek_bar), numberOfAddDrills); 
    SeekBarPlus divSeekBar1 = SeekBarPlus((SeekBar) findViewById(R.id.div_seek_bar), numberOfAddDrills); 

    } 
} 

我不知道如何將它萊特!任何建議,將不勝感激!!!

+0

將不起作用,因爲setText(int)預計資源ID – njzk2

回答

0

嘗試使用setter方法,而不是構造函數在您的自定義搜索欄:

public void setTextView(TextView text) { 
    numberOfDrills = text; 
} 

然後在您的活動,請使用

SeekBarPlus subSeekBar = (SeekBarPlus) findViewById(R.id.sub_seek_bar); 
subSeekBar.setTextView(numberOfAddDrills); 

你也可以正常使用搜索欄與OnSeekBarChangeListener:

SeekBar subSeekBar = (SeekBar) findViewById(R.id.sub_seek_bar); 
subSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { } 

      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, 
        boolean fromUser) { 
       numberOfDrills.setText(String.valueOf(progress)); 
      } 
     }); 
+0

將無法​​工作。 setText(int)需要一個資源ID – njzk2

+0

你也可以傳遞CharSequence:http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence) –

+1

是的,但在這種情況下,你可以調用setText(int),該int被解釋爲一個資源ID。 – njzk2