简体   繁体   English

Android ImageButton ClickEvent不起作用

[英]Android ImageButton ClickEvent not working

here is the code, please can anyone simply tell me how to make the onClick event run? 这是代码,请谁能简单地告诉我如何使onClick事件运行?

xml file of the activity, which has a navigation-drawer fragment, An Image view, A TextView, An ImageButton 活动的xml文件,该文件具有导航抽屉片段,图像视图,TextView和ImageButton

<?xml version="1.0" encoding="utf-8"?>


<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:orientation="vertical">


    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/drawer_layout"
        android:adjustViewBounds="true"
        android:contentDescription="@string/ContentDescriptionProfileImage"
        android:src="@drawable/profile_pic" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:padding="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="45dp">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_gravity="center_vertical"
                android:padding="3dp"
                android:text="@string/profileName"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />

        <ImageButton
            android:id="@+id/profileSettings"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="@null"
            android:contentDescription="@string/settingsIconDescription"
            android:padding="8dp"
            android:onClick="onSettingsIconPressed"
            android:src="@drawable/settings32" />
        </RelativeLayout>
    </LinearLayout>


    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/app_bar">

        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:name="studio25.materialdesign.NavigationDrawerFragment"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer"></fragment>

    </android.support.v4.widget.DrawerLayout>


</RelativeLayout>

The image of the Screen Looks like this 屏幕的图像看起来像这样

here is the activity class 这是活动课

package studio25.materialdesign;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;


public class ProfileView extends ActionBarActivity {

    private Toolbar toolbar;
    String title = "";
    ImageButton setttingsButton;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_view);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        this.getSupportActionBar().setTitle(title);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

        setttingsButton= (ImageButton) findViewById(R.id.profileSettings);


        /*
        Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(this.getResources().getColor(R.color.primaryColor));
        */
    }


    public void onSettingsIconPressed(View v)
    {
        /*
        PopupMenu popupMenu=new PopupMenu(this,v);
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.profile_settings_popup_menu,popupMenu.getMenu());
        popupMenu.show();
        */

        Toast.makeText(this,"Event Click Occured",Toast.LENGTH_SHORT).show();
    }

}

I basically wanted a menu popup on buttonclick, but on debugging realized that my button click is not itself working. 我基本上希望在buttonclick上弹出菜单,但是在调试时意识到我的按钮单击本身不起作用。

Some tutotials say for popUp menu , minSdkVersion should be 11, even changed that. 一些专家说,对于popUp菜单,minSdkVersion应该是11,甚至更改了它。

Nothing is helping me. 没有任何帮助。 Is my xml erroneous or what? 我的xml错误还是什么?

I usually like to hook up my callbacks in code (just my personal preference). 我通常喜欢用代码连接回调(仅出于个人喜好)。

setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
setttingsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(this,"clicked!",Toast.LENGTH_SHORT).show();
            }
        });

With this you can remove the onClick attribute in the xml layout. 这样,您可以删除xml布局中的onClick属性。

If you want a popup window then PopupWindow is a great option. 如果您想要一个弹出窗口,那么PopupWindow是一个不错的选择。

popupWindow = new PopupWindow(some_view_to_show, ViewGroup.LayoutParams.MATCH_PARENT, (int)popupHeight, false);
...
popupWindow.showAtLocation(view_to_inject_into, Gravity.NO_GRAVITY, 0, (size.y) - (int) popupHeight);

then you call popupWindow.dismiss(); 然后调用popupWindow.dismiss(); to remove the popup 删除弹出窗口

Your issue could also be that since you are using a RelativeLayout each element gets "stacked" on top of each other so you have essentially "buried" your ImageButton that you want to click on. 您的问题还可能是由于您使用的是RelativeLayout每个元素都被“堆叠”在彼此的顶部,因此您实际上已经“埋藏”了要单击的ImageButton So in other words some other view is blocking or consuming your clicks. 因此,换句话说,其他视图阻止或占用了您的点击次数。

Try moving your ImageButton to be one of the last elements that you add to the xml file so that it is "on top" of everything else so that it can be clicked. 尝试将ImageButton移到添加到xml文件中的最后一个元素中,以便它位于其他所有文件的“顶部”,以便可以单击它。

Alternatively you can try making the containing LinearLayout clickable instead of the ImageButton . 另外,您可以尝试使包含的LinearLayout而不是ImageButton成为可单击的。 So you would move the android:onClick="onSettingsIconPressed" attribute to the LinearLayout that contains the ImageButton . 因此,您可以将android:onClick="onSettingsIconPressed"属性移动到包含ImageButtonLinearLayout中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM