嘿所以我一直在四處搜尋這個主題,並且找不到很多,所以我想我明白地問這個問題。我想要完成的是有兩個圖像按鈕在XML佈局中重疊。然後在.java文件中點擊頂部按鈕時消失並顯示第二個圖像按鈕。點擊時,消失並且第一個圖像按鈕重新出現並且繼續。ImageButtons上的setVisibility問題
我遇到的問題是,當我點擊第一個按鈕,它自敗罰款和作品共兩次,然後周圍的一切自敗。我不知道我是否做得對,並且可能有更好的方法來實現這一點,而不是我如何做到這一點,所以我會樂於接受建議。
而且,我之所以沒有在拳按鈕,它將作爲musicbutton2可見是因爲當我這樣做,在藍色標籤的亮點,它會導致一個強制關閉的另一條線。
optionsActivity.java(新):
package com.crazycastles;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class optionsActivity extends Activity {
/** Called when the activity is first created. */
ImageButton musicbutton, musicbutton2;
boolean answer=true;
final MediaPlayer buttonSound = MediaPlayer.create(
optionsActivity.this, R.raw.swords);
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
if (answer=true){
//CREATE BUTTON 1 & SOUND
final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
musicbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
musicbutton.setBackgroundResource(R.drawable.musicbutton2);
buttonSound.start();
answer=false;
}
});
}
if (answer!=true){
//CREATE BUTTON 1 & SOUND
final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
musicbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
musicbutton.setBackgroundResource(R.drawable.musicbutton);
buttonSound.start();
answer=true;
}
});
}
}
}
options.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/mainscreen" >
<ImageButton
android:id="@+id/musicbutton"
android:layout_width="218dp"
android:layout_height="51dp"
android:layout_marginBottom="100dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="200dp"
android:background="@drawable/musicbutton"
android:scaleType="centerCrop"
/>
</LinearLayout>
如果我只用一個按鈕,不僅改變了外觀我還是能有它做例如,暫停和恢復音樂等兩種不同的操作? – 2012-01-14 17:03:17
是的,你只是保持一個布爾標誌,你可以檢查一個if語句。每次按下時,都會翻轉布爾值。在你的陳述中,你可以爲每種可能性做任何你喜歡的事情。 – FoamyGuy 2012-01-14 17:10:12
好的,謝謝你的幫助。我會嘗試。 – 2012-01-14 17:17:02