Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改菜单';s项目地点_Java_Android_Xml_Layout - Fatal编程技术网

Java 更改菜单';s项目地点

Java 更改菜单';s项目地点,java,android,xml,layout,Java,Android,Xml,Layout,如何更改该项目的位置 从这里开始: 到这里: 附言:结果是一个按钮,上面写着“完成”。 虽然这很容易,但这里的主要问题是MainActivity代码,我发现它有点难以调整 menu.xml: MainActivity.java关于“完成”项: 在布局中需要的任何位置添加按钮。绑定视图后,为关联菜单注册该视图: Button myButton = (Button) findViewById(R.id.myButton); registerForContextMenu(myButton); 可

如何更改该项目的位置

从这里开始:

到这里:

附言:结果是一个按钮,上面写着“完成”。 虽然这很容易,但这里的主要问题是MainActivity代码,我发现它有点难以调整

menu.xml:


MainActivity.java关于“完成”项:


在布局中需要的任何位置添加按钮。绑定视图后,为关联菜单注册该视图:

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);
然后覆盖以下内容以处理单击(前提是您需要与帖子中包含的功能相同的功能):

 /**
     * 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;
    }