问题描述
我落实一个活动切换波顿。我想补充的声音(上和放大器;关闭)。该按钮,但我不能添加声音就可以了。
I implement a Toggle Botton on Activity. I wanted to add Sound (on & off) to that button but I am not able to add sound on it.
这是在code我写的。
This is the code I wrote.
public class SoundLayout extends Activity implements OnClickListener
{
Button soundBttnOn;
private String _showText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_layout);
/** Fetched from the MAIN ACTIVITY */
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
_showText = bundle.getString("button_click");
}
Log.i("btnClick","button clicked is :"+_showText);
soundBttnOn = (Button) findViewById(R.id.togglebutton);
soundBttnOn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
/** make a refernce to store the intent when the view has been clicked*/
Intent intent;
/** Make cases according to the number of buttons you have in screen
* In this case, I've added one.*/
switch(view.getId()){
case R.id.togglebutton :
Log.i("btnClick","Result button is clicked whose id is :"+view.getId());
/** Intent should be fired from this activity to the other activity*/
intent = new Intent(SoundLayout.this, Main.class);
/** Start the intent
* startActivity(intent);
this.finish();*/
break;
}
}
}
在这里,我尝试添加声音的切换按钮,但我不能够添加音频功能就可以了。所以,当我点击声音开,它就会被激活,当我点击声音关闭,它就会被激活。
Here I tried to add Sound on toggle button, but I am not able to add sound function on it. So when I click on 'sound on', it gets activated and when I click on 'sound off', it gets deactivated.
推荐答案
您可以使用setOnCheckedChangeListener肘节button.In如此u得到一个paramater,指示按钮处于开启或关闭state.Based它ü可以玩使用媒体播放器兴田所需的声音。
You can make use of setOnCheckedChangeListener of toggle button.In this u get a paramater indicating if the button is in on or off state.Based on it u can play hte required sound using media player.
ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton);
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
System.out.println("ischecked>>>>>>>>>>>>>>>>>>>>>>>>>.");
else
System.out.println("not checked>>>>>>>>>>>>>>>>>>>>>>>>>.");
}
});
这篇关于如何添加声音的切换按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!