更改按钮文本和行动 - Android开发按钮、行动、文本和、Android

2023-09-05 06:35:36 作者:拉风大全

我在遇到麻烦找出如何改变一个按钮上的文字和行动。我想要做的是有文字玩,并点击时,它会播放歌曲和文本更改为暂停按钮。那么当您再次单击它,它会暂停这首歌,文本更改为玩。

我知道如何使用媒体播放器(编码),只是不知道如何为$ C C按钮$这种方式:

到目前为止,我有:

 最后按钮testButton =(按钮)findViewById(R.id.button1);
testButton.setText(播放);
testButton.setOnClickListener(新View.OnClickListener(){

@覆盖
公共无效的onClick(视图v){
mPlayer.start();
testButton.setText(暂停);
 

解决方案

您可以使用setTag。所以,你的code的样子,

 最后按钮testButton =(按钮)findViewById(R.id.button1);
testButton.setTag(1);
testButton.setText(播放);
testButton.setOnClickListener(新View.OnClickListener(){

@覆盖
公共无效的onClick(视图v){
最终诠释状态=(整数)v.getTag();
如果(状态== 1){
    mPlayer.start();
    testButton.setText(暂停);
    v.setTag(0); //暂停
} 其他 {
    testButton.setText(播放);
    v.setTag(1); //暂停
}
 
Android 开发教程

关于setTag

I'm having trouble figuring out how to change the text and action of a button. What I want to do is have a button with the text "play" and when clicked it will play a song and change the text to "pause". then when you click it again, it will pause the song and change the text to "play".

I know how to use the mediaplayer (the coding) and just don't know how to code the button that way:

so far I have:

final Button testButton = (Button) findViewById(R.id.button1);
testButton.setText("Play");
testButton.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick (View v) {
mPlayer.start();
testButton.setText("Pause");

解决方案

You can use setTag. So, your code will look like,

final Button testButton = (Button) findViewById(R.id.button1);
testButton.setTag(1);
testButton.setText("Play");
testButton.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick (View v) {
final int status =(Integer) v.getTag();
if(status == 1) {
    mPlayer.start();
    testButton.setText("Pause");
    v.setTag(0); //pause
} else {
    testButton.setText("Play");
    v.setTag(1); //pause
} 

About setTag