簡體   English   中英

Android ImageButton問題,如何通過累積按下來調用方法和祝酒

[英]Android ImageButton issues, how to call methods and toasts with cumulative pressing

我有一個小丑鳴喇叭應用程序。 當用戶按下占據整個屏幕的圖像按鈕時,它將播放持續半秒鍾的媒體播放器聲音。

我想讓它演奏說“ HONK”,“ HONK !!!”和“ HONK !!!!!!!”的祝酒詞 並保留一個計數變量,當該計數達到(%10 == 0)和/或(%100 == 0)語句時,會敬酒AMAZING HONKS。

我添加了一個觸摸和一個onbuttonlistener,而我的計數卻一無所獲。 我也想讓imagebutton占據整個屏幕並在imagebutton的頂部有一個textview ...

發布我的MAIN和XML:

public class MainActivity extends Activity implements OnTouchListener{

public void amazinghonks(View view)
{
    MediaPlayer mp = MediaPlayer.create(this, R.raw.bikehorn);
    mp.start();
}

private SoundPool soundPool;
private int soundID;
private int soundID2;
boolean loaded = false;
boolean songloaded = false;
int honkcount = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);
    View view = findViewById(R.id.toptext);
    view.setOnTouchListener(this);
    // Set the hardware buttons to control the music
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    // Load the sound
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                int status) {
            loaded = true;
        }
    });
    soundID = soundPool.load(this, R.raw.bikehorn, 1);
    soundID2 = soundPool.load(this, R.raw.clown, 1);
    Toast.makeText(MainActivity.this,
            "HONK!!!", Toast.LENGTH_LONG).show();
}

@Override
public boolean onTouch(View v, MotionEvent event) {



    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // Getting the user sound settings
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;
        // Is the sound loaded already?
        if (loaded && (honkcount < 3)) {
            honkcount++;
            soundPool.play(soundID, volume, volume, 1, 0, 1f);
            Log.e("Test", "Played sound");
        }

            if (songloaded && (honkcount > 10)) {
                honkcount = 0;
                soundPool.play(soundID2, volume, volume, 1, 0, 1f);
                Log.e("Test", "Played sound");
            }




    }
    return false;
}




public void addListenerOnButton() {

    ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);

    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            double randomnumber = Math.random()*101;
       //WILL HAVE THREE KINDS OF HONKS!     
            if(randomnumber < 33)
            {
           Toast.makeText(MainActivity.this,
            "HONK!", Toast.LENGTH_SHORT).show();

            }
            if((randomnumber < 66)&&(randomnumber > 33))
            {
           Toast.makeText(MainActivity.this,
            "HONK!!!", Toast.LENGTH_SHORT).show();
            }
            if((randomnumber < 101)&&(randomnumber > 66))
            {
           Toast.makeText(MainActivity.this,
            "HONK!!!!!!!!!!!!!!!!!!!!", Toast.LENGTH_SHORT).show();
            }


        }

    });

}

}


<?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" >

<TextView
    android:id="@+id/toptext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#303030"
    android:text="@string/toptext"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#33B5E5" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="0.62"
    android:contentDescription="@string/honktext"
    android:onClick="amazinghonks"
    android:src="@drawable/big_clown" />

</LinearLayout>

據我所知,您永遠不會調用addListenerOnButton()方法,因此它永遠不會執行。 嘗試在onCreate()方法中調用它。

為了使ImageButton能夠占據整個視圖,並在其頂部具有TextView (如在更大的z-index中,對嗎?),您想使用RelativeLayout而不是LinearLayout 並嘗試將ImageButton的高度設置為“ fill_parent”而不是0dip。 高度0將有效隱藏它,使其無法單擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM