java - How to combine two onClick for change text of Button and play/stop sound
问题描述
How to combine two functions in one button? How to make when button clicked - the button text change and sound will play
Now I can only play the sound or just change the button text
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button one;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button one = (Button) this.findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
String ButtonText=one.getText().toString();
if(ButtonText.equals("Stop")){
one.setText("Play");
}else {
one.setText("Stop");
}
}
});
解决方案
您似乎已经在onClick
事件处理程序中更改了按钮文本。如果您正在使用某些方法调用来播放所需的声音,您也可以将其放入您的onClick
处理程序方法中。例如:
public void onClick(View v) {
String ButtonText=one.getText().toString();
// method call that plays the sound
if(ButtonText.equals("Stop")){
one.setText("Play");
} else {
one.setText("Stop");
}
}
推荐阅读
- c# - 基于接口的全局过滤器
- java - Spring Boot + ActiveMQ 以编程方式即时订阅主题
- javascript - 内联表在 IE 上无法正常工作
- python - 通过 .txt 文件解析以创建制表符分隔的输出文件
- java - 将表示数组的 JSON 转换为 String[]
- itext - 为什么 itext5 会生成通过 Acrobat Reader 看不到的文本?
- django - MultipleObjectsReturned 在复杂的查询集中,而结果应该是唯一的
- vue.js - vue js中“v-bind:value”和“v-bind:key”的区别
- node.js - 与路由中的回调作斗争 - NodeJS
- python - Python 3 Tkinter - 尝试通过一个按钮阻止多个窗口打开