简体   繁体   English

操作栏菜单项onclick?

[英]actionbar menu item onclick?

I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up. 我有一个操作栏,将所有内容放在右上角的菜单中,用户点击该菜单并打开菜单选项。

I inflate the action bar menu with this on each activity I use it: 我在使用它的每个活动上用操作栏菜单膨胀:

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

        return true;
    }

And my xml for main2.xml is: 我的main2.xml的xml是:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_searchHome"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Seach"/>



</menu>

My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? 我的问题是我在xml中的项目中放了一个onclick,如果是这样,我在哪里放置它调用的onclick方法? Do I need to put it in every activity I launch this action bar in? 我是否需要将其添加到我启动此操作栏的每个活动中?

If you add an onClick attribute on your menu item like this: 如果在菜单项上添加onClick属性,如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_searchHome"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:onClick="doThis"
        android:title="Seach"/>



</menu>

Then in your activity: 然后在你的活动中:

public void doThis(MenuItem item){
    Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}

Note: 注意:

ActionBarSherlock is deprecated . 不推荐使用 ActionBarSherlock。 Unless you are developing an app for Android 4.0 or older, please don't use it. 除非您正在开发适用于Android 4.0或更早版本的应用,否则请不要使用它。 But if you are using the library, you will have to import 但是如果您使用的是库,则必须导入

import com.actionbarsherlock.view.MenuItem;

and not 并不是

import com.android.view.MenuItem;

In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick 此外,您可以执行以下操作: ActionBar Sherlock菜单项OnClick

which @adneal mentions. @adneal提到的。

In my opinion 在我看来

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    add_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onCreateDialog(getTaskId());
        }
    });
}


<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/add_text_id" android:title="Add"
    android:icon="@drawable/ic_add_btn"
    android:orderInCategory="100" app:showAsAction="ifRoom" />

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

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