将OnClickListener设置为ImageButton

时间:2017-07-20 14:41:38

标签: java android xml

我尝试将.postid-2916 > .vimeo-wrapper { width:100%; position: relative; margin: 0 auto; border: 1px solid green; padding-bottom:10px; } .vimeo-wrapper >.vimeo-video-1 { float: left; height: 300px; width: 50%; } .vimeo-wrapper >.vimeo-video-2 { float: right; height: 300px; width: 50%; } .postid-2916 iframe { width: 100%; height: 100%; } .postid-2916 @media (max-width:767px) { .postid-2916 .vimeo-standard { float: none; width: 50%; margin: 0 auto; padding-bottom: 10px; } } .postid-2916 .clearfix:before, .clearfix:after { content: " "; /* 1 */ display: table; /* 2 */ } .postid-2916 .clearfix:after { clear: both; } 设置为我的OnClickListener,但它无效。我已经看到了关于这个主题的其他问题,但没有一个ansqers对我有用。我做错了吗?

MainActivity.java

imageButton

activity_main.xml中

 public void Button()
    {
        View.OnClickListener listSet = new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();

            }
        };
        boton= (ImageButton) findViewById(R.id.imageButton7);
        boton.setOnClickListener(listSet);
    }

3 个答案:

答案 0 :(得分:0)

ImageButton的可点击属性设为true

<ImageButton
    android:id="@+id/imageButton7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:background="@android:color/background_light"
    android:src="@drawable/ic_gear"
    tools:background="@android:color/background_light"
    tools:ignore="ContentDescription" />

答案 1 :(得分:0)

试试这个:

  ImageButton ib = (ImageButton) ad.findViewById(R.id.imageButton7);
        ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_SHORT).show();
            }
        });

答案 2 :(得分:0)

尝试:

    boton= (ImageButton) findViewById(R.id.imageButton7);
    boton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Toast.makeText(this, "settings", Toast.LENGTH_LONG).show();

        }
    };);