ActionBar设置双击事件

package com.example.actionbartest;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.ActionBar;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    long[] mHits = new long[2];// 数组长度表示单击次数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getActionBar();
        View customView = LayoutInflater.from(this).inflate(R.layout.test_title, new LinearLayout(this), false);
        getActionBar().setDisplayShowHomeEnabled(false);                            
        getActionBar().setDisplayShowCustomEnabled(true);
        getActionBar().setCustomView(customView);
        customView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {  
                doubleClick();
                System.out.println("自定义的ActionBar被单击了");
            }

            private void doubleClick() {
                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();// 系统时钟, 开机后开始计算,
                                                                        // 开机时间
                if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
                    Toast.makeText(getApplicationContext(), "我被双击了", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @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;
    }

}

布局文件随意写个activity即可(测试用)

你可能感兴趣的:(Actionbar)