通过菜单单击更改字符串

Change String by Menu Click

我的 android 工作室项目中有 Constant.java。我的应用程序有一个菜单。我想通过单击菜单项更改字符串。我该怎么做?

这是我的 constant.java 文件。

public class Constant implements Serializable{

    /**
     *
     */


    private static final long serialVersionUID = 1L;

    //server url
    public static final String SERVER_URL= BuildConfig.server_url;


    public static final String IMAGE_PATH_URL =SERVER_URL+"images/";

    public static final String CATEGORY_URL = SERVER_URL+"api.php?cat_list";
}

这是我的 drawer_menu.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/menu_go_home"
            android:checked="true"
            android:icon="@drawable/ic_d_home"
            android:title="@string/menu_home" />

        <item
            android:id="@+id/menu_go_category"
            android:checked="false"
            android:icon="@drawable/ic_d_category"
            android:title="@string/menu_category" />
        <item
            android:id="@+id/menu_go_diziler"
            android:checked="false"
            android:icon="@drawable/ic_d_category"
            android:title="Diziler" />

        <item
            android:id="@+id/menu_go_latest"
            android:checked="false"
            android:icon="@drawable/ic_d_latest"
            android:title="@string/menu_latest" />

        <item
            android:id="@+id/menu_go_all"
            android:checked="false"
            android:icon="@drawable/ic_d_video"
            android:title="@string/menu_all" />

        <item
            android:id="@+id/menu_go_favourite"
            android:checked="false"
            android:icon="@drawable/ic_d_favorite"
            android:title="@string/menu_favorite" />

        <item
            android:id="@+id/menu_go_rate"
            android:checkable="false"
            android:icon="@drawable/ic_d_rate"
            android:title="@string/menu_rate" />

        <item
            android:id="@+id/menu_go_share"
            android:checkable="false"
            android:icon="@drawable/ic_d_share"
            android:title="@string/menu_share" />
        <item
            android:id="@+id/menu_go_more"
            android:checkable="false"
            android:icon="@drawable/ic_d_more"
            android:title="@string/menu_more" />

        <item
            android:id="@+id/menu_go_about"
            android:checked="false"
            android:icon="@drawable/ic_d_about"
            android:title="@string/menu_about" />

        <item
            android:id="@+id/menu_go_privacy"
            android:checked="false"
            android:icon="@drawable/ic_d_privacy"
            android:title="@string/menu_privacy" />



    </group>
</menu>

现在,例如,当我单击带有 android:id="@+id/menu_go_category" ID 的菜单项时,我想更改名为 Constant.java 的字符串 public static final String CATEGORY_URL = SERVER_URL+"api.php?cat_list";public static final String CATEGORY_URL = SERVER_URL+"api.php?menu_go_category (or anything else)

我该怎么做?

注意:我的 Constant.java 文件和 drawer_menu.xml 文件在不同的包中

试试这个。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.drawer_menu, menu);
  return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  // Handle item selection
  switch (item.getItemId()) {
      case R.id.menu_go_home:
          String itemTitle = item.getTitle();
          Constant.CATEGORY_URL = SERVER_URL+"api.php? + itemTitle;
          return true; 
       default:
          return super.onOptionsItemSelected(item);
  }
}