2015-10-18 45 views
1

我正在做一些測試,只是基於三個滑塊來改變文本顏色作爲我在主要活動中的RGB值。問題在於無法在我的滑塊上設置自定義偵聽器。這是我的代碼。另外,需要的所有東西都是導入的,只是沒有包含它們,因爲這裏的代碼插入事物並不喜歡它;無法解析setOnSeekBarChangeListener()

public class MainActivity extends AppCompatActivity { 
int[] currentCol = {0, 0, 0}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


} 
SeekBar Red = (SeekBar) findViewById(R.id.R); 
SeekBar Blue = (SeekBar) findViewById(R.id.B); 
SeekBar Green = (SeekBar) findViewById(R.id.G); 

TextView textBox = (TextView) findViewById(R.id.colorText); 


OnSeekBarChangeListener customListener = new OnSeekBarChangeListener() { 

    @Override 
    public void onProgressChanged(SeekBar name, int progress, boolean fromUser) { 
     if (name == Red) { 
      currentCol[0] = progress; 
     } else if (name == Blue) { 
      currentCol[1] = progress; 
     } else if (name == Green) { 
      currentCol[2] = progress; 
     } 
     textBox.setTextColor(Color.rgb(currentCol[0], currentCol[1], currentCol[2])); 
    } 


    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) { 

    } 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) { 

    } 

}; 

public void setCustomListener(OnSeekBarChangeListener customListener) { 
    this.customListener = customListener; 
} 

public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


} 

回答

1

先加implements OnSeekBarChangeListener{

公共類MainActivity擴展AppCompatActivity實現 OnSeekBarChangeListener {

稱這種現象的OnCreate()函數

SeekBar Red = (SeekBar) findViewById(R.id.R); 
SeekBar Blue = (SeekBar) findViewById(R.id.B); 
SeekBar Green = (SeekBar) findViewById(R.id.G); 

Red.setOnSeekBarChangeListener(this); // set seekbar listener. 

對於實踐演示

  1. Android SeekBar Example
+1

哦。把它放在oncreate()函數中。我之前也看到了這種聯繫,但並沒有意識到把它放在那裏是必需的。我覺得現在不注意那麼多關注是愚蠢的。謝謝! –

+0

@毒氣企鵝有時會發生,很酷。 –