2012-07-21 2 views
1

하위 메뉴가 있으며 현재 선택한 상태를 표시하는 항목을 가져 오려고합니다. 하위 메뉴이므로 메뉴 메서드를 호출 할 수 없습니다. 처음 한번 선택되면 확인 된 것으로 표시되지만 메뉴를 처음으로 늘리면 표시되도록해야합니다. 어떤 아이디어라도 제발?현재 상태를 유지하지 않는 하위 메뉴

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

     } 

답변

1

알 수 있습니다. 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> 
다음

는 현재 설정을 표시합니다 (이 경우 국가에서) 선택의 하위 메뉴를 표시하는 옵션 메뉴를 만들기위한 전체 코드입니다

공백을 채울 수 있도록 여기에 충분히 표시되기를 바랍니다.

관련 문제