更改按钮文字和动作 - android 开发 [英] Change button text and action - android development

查看:23
本文介绍了更改按钮文字和动作 - android 开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何更改按钮的文本和操作.我想要做的是有一个带有播放"文本的按钮,单击它会播放一首歌曲并将文本更改为暂停".然后当你再次点击它时,它会暂停歌曲并将文本更改为播放".

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:

到目前为止我有:

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

推荐答案

您可以使用 setTag.所以,你的代码看起来像,

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

关于setTag

这篇关于更改按钮文字和动作 - android 开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆