gpt4 book ai didi

android - 自定义操作栏菜单项一键不响应

转载 作者:太空狗 更新时间:2023-10-29 14:05:58 25 4
gpt4 key购买 nike

我为菜单项创建了自定义布局。现在我需要它在单击时加载新 Activity 。代码逻辑如下:

资源中的菜单声明:

<item android:id="@+id/shoppingCart"
android:title="cart"
android:background="@layout/basket_notification_counter"
android:icon="@drawable/ic_add_shopping_cart_white_24dp"
app:showAsAction="always" />

Activity 由各个选项卡下的 fragment 组成。根据我在 SO 上收集到的信息,我需要在 fragment 内的 onCreateView 方法中调用 setHasOptionsMenu(true);,我已经这样做了。

现在在activity里面,2个主要的重要方法,分别是onCreateOptionsMenuonOptionsItemSelected,如下:

package project.activities;

//... Imports come here


public class SalesActivity extends ActionBarActivity
{
private final static String TAG = "PROJECT";

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sales);

// Setup the action bar
runOnUiThread(new Runnable() {
@Override
public void run() {
showActionBar();
}
});
}

/**
* Creates menus found the action bar
*
* @param menu the menu to work on
* @return true
*/

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

final MenuItem item = menu.findItem(R.id.shopping_cart);
// THIS IS THE PROBLEM
// This workd but erratcally. After a number of clicks, it loads the activity specified in onOptionsItemSelected
// This is random: sometimes one click, sometimes 2 or up to 7 clicks so far.
item.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menu.performIdentifierAction(item.getItemId(), 0);
}
});

/**
// Also tried this but didn't work. Didn't also throw an exception to tell something was wrong
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
LoggedInActivity.this.showCart();
return true;
}
});
*/

return true;
}

/**
* Handles menus in lists
*
* @param item the selected item
* @return the selected item
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
switch (id) {
case R.id.shopping_cart:
Intent intent = new Intent(LoggedInActivity.this, CheckOutActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}

/**
* When the screen is rotated, this method is called
*
* @param newConfig the new app configuration
*/
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}

/**
* Recreates an item in cases where the app is pushed to the background
*
* @param savedInstanceState the bundle
*/
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
}

/**
* Handles the action bar
*/
public void showActionBar()
{
// Initialize the action bar
ActionBar actionBar = getSupportActionBar();
//actionBar.setElevation(0);

// Set up tabs
showActionBarTabs(actionBar);
}

/**
* Setup the actionbar tabs
* @param actionBar the actionBar we get from the activity and style
*/
public void showActionBarTabs(final ActionBar actionBar)
{
//... I set up the actionbar tabs here
}
}

问题如下:单击操作栏中的菜单项时,它相当“随机”地工作。有时,它会在单击一次后工作,有时它会加载 Activity 4 次或 3 次单击。没有1次点击的一致性。似乎是什么问题?

最佳答案

请试试这个

onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)

方法

Kotlin

inflater.inflate(R.menu.my_menu, menu)
menu.findItem(R.id.my_custom_item).actionView.setOnClickListener {
println("КЛИК")
}

Java

inflater.inflate(R.menu.my_menu, menu)
findItem(R.id.my_custom_item).getActionView().setOnClickListener(v -> System.out.println("click");

对我有用

关于android - 自定义操作栏菜单项一键不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353593/

25 4 0