2012-04-04 101 views
2

我想在用戶移動它時顯示seekbar的值,這與聯繫人列表視圖顯示當您滾動時所在的部分的字母(Android 2.3.x)類似。Android:在OnSeekBarChangeListener中顯示Toast?

任何人都有如何實施它的建議?我正在想OnSeekBarChangeListener的onProgressChanged()方法中的Toast,但無法獲取上下文來顯示()Toast。

編輯:另外,Seekbar是在一行ListView中,所以OnSeekBarChangeListener是在類Extendending ArrayAdapter,而不是通常的「活動」,所以像getBaseContext()這樣的東西不可用的方法。

回答

1

你可以使用:

Toast.makeText(Main.this, "Message", Toast.LENGTH_LONG).show(); 

Toast.makeText(getBaseContext(), "Message", Toast.LENGTH_LONG).show(); 

第一種方式:用Application類全球

public class MyApp extends Application { 
    private static MyApp instance; 

    public static MyApp getInstance() { 
     return instance; 
    } 

    public static Context getContext(){ 
     return instance.getApplicationContext(); 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     instance = this; 
    } 
} 

方式二:Context到類的構造函數或至類中的方法

+0

忘了提及,我有一個ListView中的Seekbar,所以監聽器是在擴展ArrayAdapter類。我沒有getBaseContext()可用。 – wufoo 2012-04-04 21:33:26

0

感謝您的答覆。我在這兩個建議中都使用了一些,並提出了這個建議,但即使在500毫秒的時間內,Toast的更新速度也不足以跟上seekBar的變化。嗯...

public class adapListControl extends ArrayAdapter <String> 
{ 
    final Context mCtx; 
    ... 

    public adapListControl (Context context, int textViewResourceId) 
    { 
     super (context, textViewResourceId); 
     this.mCtx = context; 
    } 
    ... 

    OnSeekBarChangeListener osbl = new OnSeekBarChangeListener() 
    { 
     @Override 
     public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) 
     { 
     Log.d (TAG, " prog change:" + progress); 
     Toast.makeText(mCtx, "" + progress, 500).show(); 
     } 
     ... 
    }