没有对setVisibility的响应

时间:2015-06-20 07:51:24

标签: java android

我正在尝试使用触发声音的按钮编写应用程序并希望按钮仅在按下时可见我添加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

1 个答案:

答案 0 :(得分:0)

尝试 - v.setVisibility(View.VISIBLE);

您正在制作INVISIBLE按钮,然后它将如何获得点击事件。你必须让它可见才能执行点击事件。 代码 -

 btn.setVisibilty(View.VISIBLE);
 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MyAndroidAppActivity.this,
            "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
                 v.setVisibility(View.INVISIBLE);

                MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.kick);
                mp.start();

        }
});

并在布局中添加以下行 -

  android:clickable="true"