安卓的onClick在xml的菜单项设置 [英] android:onClick set on menuitem in xml

查看:169
本文介绍了安卓的onClick在xml的菜单项设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,我们可以用菜单项的onclick使用XML。如下图所示。我尝试过,但它不工作。

Dear can we use onclick with menu item in xml. like below .i tried it but its not working.

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

<item
    android:id="@+id/logout"
    android:onClick="onLogOut"
    android:title="@string/logout">
</item>

Java的code:

Java Code :

public void onLogOut(MenuItem v) {
    Utility.LogError(TAG, "onLogOut Clicked");
    Toast.makeText(this, "onLogOut", Toast.LENGTH_SHORT).show();
}

}

onLogOut不被调用....

onLogOut is not get called....

推荐答案

使用

android:onClick="onLogOutClick"

而不是

android:onClick="@string/onLogOutClick"

对于菜单项的onclick加入

for adding onclick with menu item

和而在Java中,我们应该为这个点击数方法

and And in Java we should write the method for this onlick

public void onLogOutClick(MenuItem item) {
     Log.d("MenuItem", "onLogOutClick :: "+item.getItemId());  
}

这篇关于安卓的onClick在xml的菜单项设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆