gpt4 book ai didi

android - 如何在切换按钮上添加声音?

转载 作者:行者123 更新时间:2023-11-29 16:06:10 25 4
gpt4 key购买 nike

我在 Activity 上实现了一个 Toggle Botton。我想为该按钮添加声音(开和关),但无法在其上添加声音。

这是我写的代码。

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;
}
}

}

在这里,我尝试在切换按钮上添加声音,但我无法在其上添加声音功能。因此,当我点击“开启声音”时,它会被激活,而当我点击“关闭声音”时,它会被停用。

最佳答案

您可以使用切换按钮的 setOnCheckedChangeListener。在此您会得到一个参数,指示按钮是处于打开还是关闭状态。基于它,您可以使用媒体播放器播放所需的声音。

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>>>>>>>>>>>>>>>>>>>>>>>>>.");

}
});

关于android - 如何在切换按钮上添加声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18144522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com