Java 在Longclick上打开Actionbar菜单-Android
我试图使actionbar菜单(onCreateOptions菜单)只需长时间单击即可打开。我将如何实现这一点?目前,我只需使用以下代码进行一次短按/单击即可使用它:Java 在Longclick上打开Actionbar菜单-Android,java,android,menu,android-actionbar,menubar,Java,Android,Menu,Android Actionbar,Menubar,我试图使actionbar菜单(onCreateOptions菜单)只需长时间单击即可打开。我将如何实现这一点?目前,我只需使用以下代码进行一次短按/单击即可使用它: @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // TODO: Only onlongclick s
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// TODO: Only onlongclick should the menu be revealed
getMenuInflater().inflate(R.menu.my_menu_id, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
open_home();
return true;
case R.id.menu_how_to:
open_how_to();
return true;
case R.id.menu_rate:
open_rate();
return true;
case R.id.menu_about:
open_about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我希望菜单只打开一次长时间的点击(有点像隐藏/特技功能)。我已经调查过onLongClickListener,但无法让它发挥作用。菜单打开后,可以正常运行(无需长时间单击)。谢谢你的帮助!非常感谢。这是不可能的。操作栏按钮上的长单击不可覆盖
它总是会显示一个祝酒词,上面写着你长时间按下的按钮的名字。这是帮助和发现功能。这是不可能的。操作栏按钮上的长单击不可覆盖
它总是会显示一个祝酒词,上面写着你长时间按下的按钮的名字。这是一个帮助和发现功能。好的,根据您是否需要菜单中的图标,有两个答案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:paddingLeft="16dp"/>
<TextView
android:id="@+id/title"
android:text=""
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff6f6f6"
android:textSize="18sp"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"/>
</LinearLayout>
// This goes somewhere in the activity's onCreate
ImageButton menu_home_button = (ImageButton) findViewById(R.id.action_bar_menu_home);
registerForContextMenu(menu_home_button); //ONLY USE THIS FOR METHOD1
menu_home_button.setLongClickable(true);
menu_home_button.setClickable(true);
// Custom action bar
actionBar = getActionBar();
actionBar.setCustomView(R.layout.my_custom_actionbar);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
My ImageButton覆盖在右侧的操作栏顶部,在My_custom_actionbar.xml中看起来像这样:
<ImageButton
android:background="?android:selectableItemBackground"
android:id="@+id/action_bar_menu_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/menu_icon"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="4dp"
android:clickable="true"
android:focusable="true"
android:longClickable="true" />
下面的代码将显示OnCreateOptions菜单和OnOptions项Selected的位置(在java类的末尾——我的名为WebViewActivity.java)
然后在my_menu_id.xml中创建菜单。在XML中包含图标不起作用,因此不必费心尝试:
-----------------------------------------
方法2:
将此添加到WebViewActivity.java中的onCreate:
// Do the long press stuff
menu_home_button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Long Press works...", Toast.LENGTH_SHORT).show();
// Do whatever you want on the longclick
}
});
menu_home_button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Menu text
final String[] items = {"Home", "How To", "Rate", "About"};
// Menu Icons in Drawable
final Drawable[] item_icons = {
getResources().getDrawable(R.drawable.home_icon),
getResources().getDrawable(R.drawable.how_to_icon),
getResources().getDrawable(R.drawable.rate_icon),
getResources().getDrawable(R.drawable.about_icon),
};
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.custom_menu_dialog, items) {
ViewHolder holder;
class ViewHolder {
ImageView icon;
TextView title;
}
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.custom_menu_dialog, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
// view already defined, retrieve view holder
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(items[position]);
holder.icon.setImageDrawable(item_icons[position]);
return convertView;
}
};
Toast.makeText(getApplicationContext(), "Long Press works...", Toast.LENGTH_SHORT).show();
AlertDialog.Builder menu_dialog = new AlertDialog.Builder(WebViewActivity.this);
menu_dialog.setTitle(getResources().getString("My Menu Name");
menu_dialog.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
//Toast.makeText(WebViewActivity.this, "You selected: " + items[item], Toast.LENGTH_LONG).show();
switch (item) {
case 0:
open_home();
break;
case 1: // HOW TO
open_how_to();
break;
case 2:
open_rate();
break;
case 3: // ABOUT
open_about();
break;
default: // Do Case 0
open_home();
break;
}
dialog.dismiss();
}
});
AlertDialog alert = menu_dialog.create();
alert.show();
return true;
}
});
菜单\主菜单\按钮。设置OnLongClickListener(新视图。OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
//菜单文本
最后一个字符串[]items={“Home”、“How To”、“Rate”、“About”};
//可绘制的菜单图标
最终可绘制[]项目图标={
getResources().getDrawable(R.drawable.home_图标),
getResources().getDrawable(R.drawable.how_to_图标),
getResources().getDrawable(R.drawable.rate_图标),
getResources().getDrawable(R.drawable.about_图标),
};
ListAdapter=new ArrayAdapter(getApplicationContext(),R.layout.custom_菜单_对话框,项){
视窗座;
类视图持有者{
图像视图图标;
文本视图标题;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
最终LayoutFlater充气器=(LayoutFlater)getApplicationContext().getSystemService(Context.LAYOUT\u充气器\u服务);
if(convertView==null){
convertView=充气机。充气(R.layout.custom_菜单对话框,空);
holder=新的ViewHolder();
holder.icon=(ImageView)convertView.findViewById(R.id.icon);
holder.title=(TextView)convertView.findViewById(R.id.title);
convertView.setTag(支架);
}否则{
//视图已定义,检索视图持有者
holder=(ViewHolder)convertView.getTag();
}
持有者.头衔.setText(项目[位置]);
支架图标可设置图像绘制(项目图标[位置]);
返回视图;
}
};
Toast.makeText(getApplicationContext(),“长按工作…”,Toast.LENGTH\u SHORT.show();
AlertDialog.Builder菜单\对话框=新建AlertDialog.Builder(WebViewActivity.this);
菜单对话框.setTitle(getResources().getString(“我的菜单名”);
菜单\对话框.setAdapter(适配器,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int项){
//Toast.makeText(WebViewActivity.this),您选择:“+items[item],Toast.LENGTH\u LONG.show();
开关(项目){
案例0:
打开家();
打破
案例1://如何
打开_how_to();
打破
案例2:
打开速率();
打破
案例3://关于
打开关于;
打破
默认值://Do Case 0
打开家();
打破
}
dialog.dismise();
}
});
AlertDialog alert=菜单_dialog.create();
alert.show();
返回true;
}
});
最后,这里是custom_menu_dialog.xml文件,其中包含菜单文本和图标的基本信息:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:paddingLeft="16dp"/>
<TextView
android:id="@+id/title"
android:text=""
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff6f6f6"
android:textSize="18sp"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"/>
</LinearLayout>
希望这至少能帮助其他人。这对我来说确实有效。好的,根据您是否希望菜单中有图标,这里有两个答案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:paddingLeft="16dp"/>
<TextView
android:id="@+id/title"
android:text=""
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff6f6f6"
android:textSize="18sp"
android:paddingLeft="16dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"/>
</LinearLayout>