gpt4 book ai didi

Android:Actionbar item onclick

转载 作者:太空狗 更新时间:2023-10-29 16:38:22 25 4
gpt4 key购买 nike

1-将搜索项添加到我的 actionbar 我有这样的项目:

<item 

android:id="@+id/search"
android:icon="@drawable/ic_action_search"
android:title="@string/Search"
android:showAsAction="ifRoom"
/>

//onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);

MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);

return super.onCreateOptionsMenu(menu);
}

//OnOptionItemSelected

public boolean OnOptionItemSelected(MenuItem item)
{

switch(item.getItemId())
{

case R.id.search:
action_search();
return true;

default:
return super.onOptionsItemSelected(item);


}
}

//搜索 Action

public void action_search()
{

System.out.println("Heeeeey");

}

,但是如果我添加这个 android:onClick="action_search" 它会给我这些错误

03-20 07:40:12.622: E/Trace(1277): error opening trace file: No such file or directory (2)
03-20 07:40:13.002: E/AndroidRuntime(1277): FATAL EXCEPTION: main
03-20 07:40:13.002: E/AndroidRuntime(1277): android.view.InflateException: Couldn't resolve menu item onClick handler action_search in class com.example.lesson1.MainActivity
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:217)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:417)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater.parseMenu(MenuInflater.java:188)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater.inflate(MenuInflater.java:110)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.example.lesson1.MainActivity.onCreateOptionsMenu(MainActivity.java:30)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2913)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.os.Handler.handleCallback(Handler.java:615)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.os.Looper.loop(Looper.java:137)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-20 07:40:13.002: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-20 07:40:13.002: E/AndroidRuntime(1277): at dalvik.system.NativeStart.main(Native Method)
03-20 07:40:13.002: E/AndroidRuntime(1277): Caused by: java.lang.NoSuchMethodException: action_search [interface android.view.MenuItem]
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-20 07:40:13.002: E/AndroidRuntime(1277): at java.lang.Class.getMethod(Class.java:915)
03-20 07:40:13.002: E/AndroidRuntime(1277): at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:215)
03-20 07:40:13.002: E/AndroidRuntime(1277): ... 18 more

2-什么是 android:showAsAction="ifRoom"?它在做什么?

最佳答案

1.您不应该以这种方式将 onClicks 添加到 ActionMenu 项目。相反,您必须像这样覆盖 onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.search:
// your action goes here
return true;
default:
return super.onOptionsItemSelected(item);
}
}

然后将您的布局膨胀到 ActionBar 中,如下所示:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}

2. ifRoom 表示如果有空间,将操作项放在操作栏上。但是,空间由以下因素决定:小于操作栏水平空间宽度的一半,并且计数小于操作项的最大数量 - Jake Wharton。

Have a look here for more information on the android ActionBar

关于Android:Actionbar item onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22526498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com