我試圖用觸發聲音的按鈕編寫應用程序,並希望按鈕僅在按下時纔可見我添加了setVisibility方法,但是當我按下按鈕時沒有響應。對setVisibility沒有反應
我的代碼是:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
public class MainActivity extends Activity {
RelativeLayout background;
ImageButton btn;
MediaPlayer sound;
@Override
protected void onPause() {
super.onPause();
sound.release();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sound = MediaPlayer.create(this, R.raw.kick);
btn = (ImageButton) findViewById(R.id.btn1);
btn.setVisibility(INVISIBLE);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.kick);
btn.setVisibility(VISIBLE);
mp.start();
}
});
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/background"
android:background="@drawable/fuckyou"
android:orientation="horizontal">
<ImageButton
android:id="@+id/btn1"
android:layout_width="109dp"
android:layout_height="89dp"
android:scaleType="fitXY"
android:layout_marginTop="26dp"
android:src="@mipmap/oneeee"
android:background="@null"
android:contentDescription="@null"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="1dp" />
logcat的顯示如下:
06-20 02:02:57.249 28473-28473/sp.umavibe.com.sp404 I/art﹕ Not late- enabling -Xcheck:jni (already on)
06-20 02:02:57.249 28473-28473/sp.umavibe.com.sp404 I/art﹕ Late-enabling JIT
06-20 02:02:57.251 28473-28473/sp.umavibe.com.sp404 I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
06-20 02:02:57.673 28473-28484/sp.umavibe.com.sp404 I/art﹕ Background sticky concurrent mark sweep GC freed 8726(388KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 10MB/10MB, paused 6.293ms total 56.655ms
06-20 02:02:57.695 28473-28473/sp.umavibe.com.sp404 E/MediaPlayer﹕ Should have subtitle controller already set
06-20 02:02:57.710 28473-28489/sp.umavibe.com.sp404 D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-20 02:02:57.714 28473-28473/sp.umavibe.com.sp404 D/﹕ HostConnection::get() New Host Connection established 0xab1704a0, tid 28473
06-20 02:02:57.728 28473-28473/sp.umavibe.com.sp404 E/MediaPlayer﹕ Should have subtitle controller already set
06-20 02:02:57.775 28473-28489/sp.umavibe.com.sp404 D/﹕ HostConnection::get() New Host Connection established 0xab170540, tid 28489
06-20 02:02:57.804 28473-28489/sp.umavibe.com.sp404 I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-20 02:02:57.857 28473-28489/sp.umavibe.com.sp404 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-20 02:02:57.857 28473-28489/sp.umavibe.com.sp404 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xab163c00, error=EGL_SUCCESS
線嘗試刷新父佈局,其中,所述按鈕所在。 Relativelayout.requestLayout()。 –