如何在自定义ActionBar上获取onClickListener()事件

16

我正在开发一个应用程序,需要在动作栏自定义视图点击时获取onClick()事件。目前我能够实现以下布局。

Custom Action Bar

这是我实现它的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    getActionBar().setCustomView(R.layout.custom_image_button);
    getActionBar().setDisplayOptions(
            ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case android.R.id.home:
        Toast.makeText(getApplicationContext(), "Clicked on ActionBar",
                Toast.LENGTH_SHORT).show();

    default:
        return super.onOptionsItemSelected(item);
    }
}

这是我的自定义图像按钮布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" >

    <TextView
        android:id="@+id/points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/points_yellow"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:textColor="#887141"
        android:textIsSelectable="false"
        android:textSize="22sp"
        android:textStyle="bold" >
    </TextView>

    <ImageView
        android:id="@+id/badge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:layout_marginRight="5dp"
        android:layout_marginTop="0dp"
        android:src="@drawable/badge_notification" >
    </ImageView>
</FrameLayout>

</RelativeLayout>

我试图在自定义布局上添加点击监听器。为此,我尝试了以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    getActionBar().setCustomView(R.layout.custom_image_button);
    getActionBar().setDisplayOptions(
            ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);

    final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v = inflater.inflate(R.layout.custom_image_button, null);

    frameLayout = (FrameLayout) v.findViewById(R.id.frame_layout);

    frameLayout.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(getApplicationContext(), "Clicked on 1",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case android.R.id.home:
        Toast.makeText(getApplicationContext(), "Clicked on ActionBar",
                Toast.LENGTH_SHORT).show();

    default:
        return super.onOptionsItemSelected(item);
    }
}

}

但是,我无法在自定义图像上获取onClick()事件。在这里我做错了什么,请指导。

任何帮助都将不胜感激。

2个回答

20

在Inflater中,它就像布局文件中的视图

因此,您需要在ActionBar自定义布局文件中添加 android:onClick="clickEvent"

enter image description here

这是演示:

我的MainActivity:

package com.example.testdemo;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private View viewList;
    private Dialog dialogMarketList;
    String a[] = { "a", "aa" };
    private View header;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        View cView = getLayoutInflater().inflate(R.layout.header, null);
        actionBar.setCustomView(cView);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void clickEvent(View v) {
        if (v.getId() == R.id.button1) {
            Toast.makeText(MainActivity.this, "you click on button1",
                    Toast.LENGTH_SHORT).show();
        }

        if (v.getId() == R.id.button2) {
            Toast.makeText(MainActivity.this, "you click on button2",
                    Toast.LENGTH_SHORT).show();
        }

        if (v.getId() == R.id.textView1) {
            Toast.makeText(MainActivity.this, "you click on textView1",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

我的布局 header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickEvent"
        android:text="Button 1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickEvent"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:text="My action bar"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="clickEvent"
        android:text="Button 2" />

</LinearLayout>

嘿,我在我的主xml文件中使用DrawerLayout,然后根据您的建议,我使用自定义布局来使用Actionbar。但是当我尝试使用您的代码时,我的DrawerLayout不起作用,因为actionBar.setDisplayShowCustomEnabled(true); 如果我不写这行代码,则无法在我的操作栏中看到自定义布局。请建议我正确的方法。我想同时使用两者。谢谢。 - anddev
请查看此链接:http://stackoverflow.com/questions/17161609/how-to-reduce-width-of-actionbar-menu-list-in-android你对这个问题有什么想法吗?如果我找到了解决方案,就不需要使用自定义布局了。请帮帮我,我卡在这里了。谢谢。 - anddev
但是我遇到了一个错误,它说找不到这个方法(我的点击方法),然后应用程序就关闭了,为什么? - Mahdi
是的,我这样做了,但我不知道为什么充气布局无法达到那个效果。我通过以下方式为我的操作栏创建自定义视图:@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSherlock().getMenuInflater();inflater.inflate(R.menu.main_menu, menu); menu.add(Menu.NONE, 0, Menu.NONE, "custom") .setActionView(R.layout.header) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return true;} - Mahdi
@DhawalSodhaParmar 你看到了吗? - Mahdi

6
    View cView = getLayoutInflater().inflate(R.layout.header, null);
    cView.findViewById(R.id.btn_id).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
                         // Do stuff here.
        }
    });
    actionBar.setCustomView(cView);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接