gpt4 book ai didi

android - 不保留当前状态的子菜单项

转载 作者:行者123 更新时间:2023-11-30 04:06:21 26 4
gpt4 key购买 nike

我有一个子菜单,并试图让项目显示当前选择的状态。因为它是一个子菜单,所以我不能调用菜单方法。第一次选择后它显示为已选中,但我需要在菜单首次膨胀时显示它。有什么想法吗?

public boolean onCreateOptionsMenu(Menu menu){
MenuInflater minf= getMenuInflater();
minf.inflate(R.menu.menu,menu);
return true;
}



public boolean onOptionsItemSelected(MenuItem item){

switch (item.getItemId()){
//-------Options menu----------
case R.id.about:
Intent intentA = new Intent(FuelMoney.this, About.class);
startActivity(intentA);
return true;
case R.id.locale:
return true;

//-----Sub menu---------- UK item not showing as clicked (rest of the code not complete yet)

case R.id.uk_item:
if(this.countryCode.equals("uk"))
{
item.setChecked(true);
}
Toast.makeText(this, "UK selected", Toast.LENGTH_SHORT).show();
this.countryCode="uk";
this.country = new Country(countryCode);
this.regionAttributes();
item.setChecked(true);
return true;
case R.id.us_item:
Toast.makeText(this, "US selected", Toast.LENGTH_SHORT).show();
this.countryCode="us";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
case R.id.eu_item:
Toast.makeText(this, "EU selected", Toast.LENGTH_SHORT).show();
this.countryCode="eu";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
case R.id.jpn_item:
Toast.makeText(this, "Japan selected", Toast.LENGTH_SHORT).show();
this.countryCode="jpn";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
case R.id.india_item:
Toast.makeText(this, "India selected", Toast.LENGTH_SHORT).show();
this.countryCode="ind";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
default :
return super.onOptionsItemSelected(item);

}

最佳答案

明白了。我需要获取 SubMenu 并对其调用方法。

下面是创建选项菜单的完整代码,该菜单显示选项子菜单(在本例中为国家/地区),还显示当前设置:

public boolean onCreateOptionsMenu(Menu menu){
MenuInflater minf= getMenuInflater();
minf.inflate(R.menu.menu,menu);
return true;
}


public boolean onOptionsItemSelected(MenuItem item){

switch (item.getItemId()){
//-------Options menu----------
case R.id.about:
Intent intentA = new Intent(myClass.this, About.class);
startActivity(intentA);
return true;
case R.id.locale:

//-----Sub menu----------
SubMenu sub = item.getSubMenu();
sub.setGroupCheckable(R.id.locale, true, true);
if(this.countryCode.equals("uk"))
{
sub.getItem(0).setChecked(true);
}else if(this.countryCode.equals("us"))
{
sub.getItem(1).setChecked(true);
}else if(this.countryCode.equals("eu"))
{
sub.getItem(2).setChecked(true);
}else if(this.countryCode.equals("jpn"))
{
sub.getItem(3).setChecked(true);
}else if(this.countryCode.equals("ind"))
{
sub.getItem(4).setChecked(true);
}


return true;

case R.id.uk_item:
this.subMenuHelper("uk");// see below for subMenuHelper
item.setChecked(true);
return true;
case R.id.us_item:
this.subMenuHelper("us");
item.setChecked(true);
return true;
case R.id.eu_item:
this.subMenuHelper("eu");
item.setChecked(true);
return true;
case R.id.jpn_item:
this.subMenuHelper("jpn");
item.setChecked(true);
return true;
case R.id.india_item:
this.subMenuHelper("ind");
item.setChecked(true);
return true;
default :
return super.onOptionsItemSelected(item);

}

}



public void subMenuHelper(String cCode)
{
this.countryCode=cCode;
this.country = new Country(this.countryCode);
this.regionAttributes(this.country);
}

还有 xml:

    <?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/locale"
android:title="@string/menu_set_region"
android:icon="@android:drawable/ic_menu_mapmode">

<!--region submenu -->
<menu>
<group android:checkableBehavior="single">
<item android:id="@+id/uk_item"
android:title="@string/uk"
/>
<item android:id="@+id/us_item"
android:title="@string/us"
/>
<item android:id="@+id/eu_item"
android:title="@string/eu"
/>
<item android:id="@+id/jpn_item"
android:title="@string/jpn"
/>
<item android:id="@+id/india_item"
android:title="@string/india"
/>
</group>
</menu>
</item>

<item android:id="@+id/about"
android:icon="@android:drawable/ic_menu_info_details"
android:title="@string/menu_about"
/>
</menu>

我希望这里有足够的内容让你可以填空:-)

关于android - 不保留当前状态的子菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11592906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com