0

目前我正在开发一个音乐播放器应用程序,我想更改按钮的文本。

这是我的代码。playM 是“播放”按钮,stopM 是停止,逻辑是当按下播放按钮时,playM 的文本从“播放”(playM 的默认文本为“播放”)变为“暂停”。然后当stopM被按下时,playM从“Pause”变为“Play”

然而,实际的行为是当我点击 playM 时将文本从播放更改为暂停,但是当我点击 stopM 时,playM 不会从暂停更改为播放。如何解决问题?谢谢

public class Adapter extends ArrayAdapter<Song> {
    Button playM = null;
    Button stopM = null;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {

            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.list_view, null);

        }

        p = items.get(position);

        if (p != null) {

            TextView tt = (TextView) v.findViewById(R.id.textView1);
            tt1 = (TextView) v.findViewById(R.id.textView2);
            Button del = (Button) v.findViewById(R.id.button1);
            playM = (Button) v.findViewById(R.id.button2);
            stopM = (Button) v.findViewById(R.id.button3);

            String title = p.getTitle();
            String singer = p.getSinger();

            if (tt != null) {
                tt.setText(title);
            }
            if (tt1 != null) {
                tt1.setText(singer);
            }

            del.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    db.deleteSong(p);
                }
            });

            playM.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //AudioManager manager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
                    //if (!manager.isMusicActive()) {
                        playM.setText("Pause");
                        Intent svc=new Intent(getContext(), Music.class);
                        svc.putExtra("uri", tt1.getText().toString());
                        getContext().startService(svc);
                    //}
                }
            });

            stopM.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    playM.setText("Play");
                    Intent svc=new Intent(getContext(), Music.class);
                    getContext().stopService(svc);
                }
            });
        }

        return v;

    }
}
4

2 回答 2

1

你在一个适配器中,所以你不能像那样保存项目视图。

最快但可能不是最好的解决方案是将 playM 设置为,final以便您能够访问侦听器中的右侧按钮。

public class Adapter extends ArrayAdapter<Song> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.list_view, null);

    }

    final Song p = items.get(position);

    if (p != null) {

        TextView tt = (TextView) v.findViewById(R.id.textView1);
        tt1 = (TextView) v.findViewById(R.id.textView2);
        Button del = (Button) v.findViewById(R.id.button1);
        final Button playM = (Button) v.findViewById(R.id.button2);
        Button stopM = (Button) v.findViewById(R.id.button3);

        String title = p.getTitle();
        String singer = p.getSinger();

        if (tt != null) {
            tt.setText(title);
        }
        if (tt1 != null) {
            tt1.setText(singer);
        }

        del.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                db.deleteSong(p);
            }
        });

        playM.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //AudioManager manager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
                //if (!manager.isMusicActive()) {
                    playM.setText("Pause");
                    Intent svc=new Intent(getContext(), Music.class);
                    svc.putExtra("uri", tt1.getText().toString());
                    getContext().startService(svc);
                //}
            }
        });

        stopM.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                playM.setText("Play");
                Intent svc=new Intent(getContext(), Music.class);
                getContext().stopService(svc);
            }
        });
    }

    return v;

}
}
于 2013-11-13T10:07:58.127 回答
0

请尝试使无效以进行更改。

 playM.setText("Play");
 playM.invalidate();
于 2013-11-13T10:04:37.317 回答