繁体   English   中英

更改菜单项的位置

[英]Change menu's item place

如何更改该项目的位置?

从这里: 在此处输入图片说明

到这里: 在此处输入图片说明

PS:结果将是一个带有“DONE”字样的按钮。 虽然这很容易,但这里的主要问题是 MainActivity 代码,我发现它有点难以调整。

菜单.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_done"
        android:orderInCategory="100"
        android:title="DONE"
        app:showAsAction="always" />
</menu>

MainActivity.java 关于“完成”项目:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_done:
            if (!TextUtils.isEmpty(actionName) && actionName.equals(Constants.LocationActions.SELECT_HOME) || actionName.equals(Constants.LocationActions.SELECT_WORK)) {
                Intent intent = new Intent();
                intent.putExtra(SRC_ADD, s_address);
                intent.putExtra(SRC_LAT, s_latitude);
                intent.putExtra(SRC_LONG, s_longitude);
                setResult(Activity.RESULT_OK, intent);
                finish();
            } else {

                if (mLastKnownLocation != null) {

                    LatLng latLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());

                    if (RIDE_REQUEST.containsKey(SRC_ADD) && RIDE_REQUEST.containsKey(DEST_ADD)) {
                        setResult(Activity.RESULT_OK, new Intent());
                        finish();
                    } else if (RIDE_REQUEST.containsKey(SRC_ADD) && !RIDE_REQUEST.containsKey(DEST_ADD)) {
                        String address = getAddress(latLng);
                        RIDE_REQUEST.put(DEST_ADD, address);
                        RIDE_REQUEST.put(DEST_LAT, latLng.latitude);
                        RIDE_REQUEST.put(DEST_LONG, latLng.longitude);
                        setResult(Activity.RESULT_OK, new Intent());
                        finish();
                    } else if (!RIDE_REQUEST.containsKey(SRC_ADD) && RIDE_REQUEST.containsKey(DEST_ADD)) {
                        String address = getAddress(latLng);
                        RIDE_REQUEST.put(SRC_ADD, address);
                        RIDE_REQUEST.put(SRC_LAT, latLng.latitude);
                        RIDE_REQUEST.put(SRC_LONG, latLng.longitude);
                        setResult(Activity.RESULT_OK, new Intent());
                        finish();
                    } else if (!RIDE_REQUEST.containsKey(SRC_ADD) && !RIDE_REQUEST.containsKey(DEST_ADD)) {
                        showAlert("Aviso", "Sua viajem está incompleta. Por favor, escolha um local da lista de sugestões ao digitar.");
                    }


                }
            }
            return true;
        case android.R.id.home:
            RIDE_REQUEST = ORIGINAL_RIDE_REQUEST;
            setResult(Activity.RESULT_OK, new Intent());
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

在布局中需要的地方添加按钮。 绑定视图后,为上下文菜单注册该视图:

Button myButton = (Button) findViewById(R.id.myButton);
registerForContextMenu(myButton);

可能值得创建以下内容:

private static final int HOME = 0;
private static final int DONE = 1;

然后覆盖以下方法以编程方式创建菜单:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, DONE, Menu.NONE, "DONE");
        menu.add(Menu.NONE, HOME, Menu.NONE, "HOME");
    }

参数为:

 /**
     * Add a new item to the menu. This item displays the given title for its
     * label.
     * 
     * @param groupId The group identifier that this item should be part of.
     *        This can be used to define groups of items for batch state
     *        changes. Normally use {@link #NONE} if an item should not be in a
     *        group.
     * @param itemId Unique item ID. Use {@link #NONE} if you do not need a
     *        unique ID.
     * @param order The order for the item. Use {@link #NONE} if you do not care
     *        about the order. See {@link MenuItem#getOrder()}.
     * @param title The text to display for the item.
     * @return The newly added menu item.
     */
    public MenuItem add(int groupId, int itemId, int order, CharSequence title);

然后覆盖以下内容以处理点击(前提是您需要与帖子中包含的功能相同的功能):

    @Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case DONE:

            if (!TextUtils.isEmpty(actionName) && actionName.equals(Constants.LocationActions.SELECT_HOME) || actionName.equals(Constants.LocationActions.SELECT_WORK)) {
                Intent intent = new Intent();
                intent.putExtra(SRC_ADD, s_address);
                intent.putExtra(SRC_LAT, s_latitude);
                intent.putExtra(SRC_LONG, s_longitude);
                setResult(Activity.RESULT_OK, intent);
                finish();
            } else {

                if (mLastKnownLocation != null) {

                    LatLng latLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());

                    if (RIDE_REQUEST.containsKey(SRC_ADD) && RIDE_REQUEST.containsKey(DEST_ADD)) {
                        setResult(Activity.RESULT_OK, new Intent());
                        finish();
                    } else if (RIDE_REQUEST.containsKey(SRC_ADD) && !RIDE_REQUEST.containsKey(DEST_ADD)) {
                        String address = getAddress(latLng);
                        RIDE_REQUEST.put(DEST_ADD, address);
                        RIDE_REQUEST.put(DEST_LAT, latLng.latitude);
                        RIDE_REQUEST.put(DEST_LONG, latLng.longitude);
                        setResult(Activity.RESULT_OK, new Intent());
                        finish();
                    } else if (!RIDE_REQUEST.containsKey(SRC_ADD) && RIDE_REQUEST.containsKey(DEST_ADD)) {
                        String address = getAddress(latLng);
                        RIDE_REQUEST.put(SRC_ADD, address);
                        RIDE_REQUEST.put(SRC_LAT, latLng.latitude);
                        RIDE_REQUEST.put(SRC_LONG, latLng.longitude);
                        setResult(Activity.RESULT_OK, new Intent());
                        finish();
                    } else if (!RIDE_REQUEST.containsKey(SRC_ADD) && !RIDE_REQUEST.containsKey(DEST_ADD)) {
                        showAlert("Aviso", "Sua viajem está incompleta. Por favor, escolha um local da lista de sugestões ao digitar.");
                    }


                }


            }
            break;
        case HOME:
            RIDE_REQUEST = ORIGINAL_RIDE_REQUEST;
            setResult(Activity.RESULT_OK, new Intent());
            finish();
            break;
        default:
            break;
    }

暂无
暂无

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

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