更改菜单项的位置

huangapple go评论76阅读模式
英文:

Change menu's item place

问题

从这里:

<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>

关于 "DONE" 项的 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);
    }
}
英文:

How to change the placement of that item ?

from here: 更改菜单项的位置

to here: 更改菜单项的位置

PS: The outcome would be a button with the word "DONE" in it.
Although this would be so easy, the main problem here is the MainActivity code which I found a bit hard to tweak.

menu.xml:

<!-- language: lang-xml -->

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

MainActivity.java regarding the "DONE" item:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_done:
if (!TextUtils.isEmpty(actionName) &amp;&amp; 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) &amp;&amp; RIDE_REQUEST.containsKey(DEST_ADD)) {
setResult(Activity.RESULT_OK, new Intent());
finish();
} else if (RIDE_REQUEST.containsKey(SRC_ADD) &amp;&amp; !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) &amp;&amp; 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) &amp;&amp; !RIDE_REQUEST.containsKey(DEST_ADD)) {
showAlert(&quot;Aviso&quot;, &quot;Sua viajem est&#225; incompleta. Por favor, escolha um local da lista de sugest&#245;es ao digitar.&quot;);
}
}
}
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);
}
}

答案1

得分: 0

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

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, "完成");
    menu.add(Menu.NONE, HOME, Menu.NONE, "主页");
}

其中参数为:

/**
 * 向菜单添加新项。该项将显示给定的标题作为其标签。
 * 
 * @param groupId 该项应该属于的组标识符。这可以用于批量状态更改的项目组。
 *                如果项不应属于任何组,则通常使用{@link #NONE}。
 * @param itemId 唯一的项目ID。如果不需要唯一ID,则使用{@link #NONE}。
 * @param order 项目的顺序。如果不关心顺序,则使用{@link #NONE}。参见{@link MenuItem#getOrder()}。
 * @param title 项目显示的文本。
 * @return 新添加的菜单项。
 */
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;
    }
}
英文:

Add a button wherever you need it in the layout. After you bind the view register that view for context menu:

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

It might be worth creating the following contants:

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

Then override the following method to create your menu programmatically:

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

With the parameters being:

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

then override the following to handle the clicks (provided that you need the same functionality as the one included in your post):

    @Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DONE:
if (!TextUtils.isEmpty(actionName) &amp;&amp; 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) &amp;&amp; RIDE_REQUEST.containsKey(DEST_ADD)) {
setResult(Activity.RESULT_OK, new Intent());
finish();
} else if (RIDE_REQUEST.containsKey(SRC_ADD) &amp;&amp; !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) &amp;&amp; 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) &amp;&amp; !RIDE_REQUEST.containsKey(DEST_ADD)) {
showAlert(&quot;Aviso&quot;, &quot;Sua viajem est&#225; incompleta. Por favor, escolha um local da lista de sugest&#245;es ao digitar.&quot;);
}
}
}
break;
case HOME:
RIDE_REQUEST = ORIGINAL_RIDE_REQUEST;
setResult(Activity.RESULT_OK, new Intent());
finish();
break;
default:
break;
}

huangapple
  • 本文由 发表于 2020年9月3日 01:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63710440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定