猿问

如何通过单击按钮更改同一活动中的布局?

Activity单击播放按钮时更改布局,然后布局ButtonAndroid Studio音乐播放器应用程序中的同一位置显示暂停。怎么可能?我是初学者所以我不知道如何使用它的方法?我无法得到答案。


拉莫斯之舞
浏览 159回答 1
1回答

饮歌长啸

我从您的问题中了解到,当您的用户点击pause按钮时,您应该隐藏pause按钮并显示play按钮,反之亦然。这实现起来比较简单。只需创建一个带有两个按钮的线性布局的布局,如下所示:<LinearLayout&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:orientation="horizontal"&nbsp; &nbsp; android:layout_gravity="center_horizontal"&nbsp; &nbsp; android:padding="5dp">&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/btnPlay"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="2dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="2dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Play"&nbsp; &nbsp; &nbsp; &nbsp; android:padding="5dp"/>&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/btnPause"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginLeft="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="2dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="2dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginRight="5dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Pause"/></LinearLayout>现在将单击侦听器设置为两个按钮并在其中处理按钮可见性,如下所示:Button btnPlay = barcodeDialog.findViewById(R.id.btnPlay);Button btnPause = barcodeDialog.findViewById(R.id.btnPause);btnPlay.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; btnPlay.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; btnPause.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }});btnPause.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; btnPlay.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; btnPause.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }});这样您就可以处理您的Play和Pause按钮的可见性。如果您需要更多信息,请回复。
随时随地看视频慕课网APP

相关分类

Java
我要回答