2012-11-07 28 views
0

我已经创建了一个样式xml来保存用户可以选择的不同样式。但是,无论用户选择什么,我的样式都默认为最后一种样式(绿色)。我错过了什么吗? 菜单类样式用户选择实现

public class UserMenu extends Activity implements OnClickListener { 
    Button preview; 
    Spinner spinnerColor; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      if("Red".equals(getIntent().getStringExtra("Theme"))); 
      { 
       setTheme(R.style.Theme_Red); 
      } 
      if("Green".equals(getIntent().getStringExtra("Theme"))); 
      { 
       setTheme(R.style.Theme_Blue); 
      } 
      if("Blue".equals(getIntent().getStringExtra("Theme"))); 
      { 
       setTheme(R.style.Theme_Green); 
      } 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_user_menu); 

      spinnerColor = (Spinner) findViewById(R.id.spinnerColorMenu); 
      //TextView Title = (TextView)findViewById(R.id.ViewModuleTitle); 

      preview = (Button)findViewById(R.id.previewButton); 
       preview.setOnClickListener(this); 



     } 

     public void onClick(View v) 


     { 
      String bgColor = spinnerColor.getSelectedItem().toString(); 
      if(bgColor.equals("Red")) 
      { 
       Intent intent = getIntent(); 
       intent.putExtra("Theme", "Red"); 
       finish(); 
       startActivity(intent); 
      } 
      else if(bgColor.equals("Blue")) 
      { 
       Intent intent = getIntent(); 
       intent.putExtra("Theme", "Blue"); 
       finish(); 
       startActivity(intent); 
      } 

      else if(bgColor.equals("Green")) 
      { 
       Intent intent = getIntent(); 
       intent.putExtra("Theme", "Green"); 
       finish(); 
       startActivity(intent); 
      } 


     } 
    } 

布局

<TextView 
     android:id="@+id/ViewModuleTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingTop="10dp" 
     android:text="@string/addModule" 
     android:textSize="22dp" 
     style="?textTitle" /> 

    <TextView 
     android:id="@+id/lableTextModuleCode" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/enterModuleCode" 
     android:layout_marginLeft="10dp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     style="?textBody"/> 

    <Spinner 
     android:id="@+id/spinnerColorMenu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:entries="@array/colorMenu"/> 

    <Button 
      android:id="@+id/previewButton" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:onClick="previewButton" 
      android:text="@string/addModule" /> 

    </LinearLayout> 

样式

<style name ="redBody"> 
     <item name="android:textColor">@color/red</item> 
     <item name ="android:background">@color/white</item> 
    </style> 

    <style name ="redTitle"> 
     <item name="android:textColor">@color/white</item> 
     <item name ="android:background">@color/red</item> 
    </style> 

    <style name ="blueBody"> 
     <item name="android:textColor">@color/darkBlue</item> 
     <item name ="android:background">@color/white</item> 
    </style> 

    <style name ="blueTitle"> 
     <item name="android:textColor">@color/white</item> 
     <item name ="android:background">@color/darkBlue</item> 
    </style> 

    <style name ="greenBody"> 
     <item name="android:textColor">@color/green</item> 
     <item name ="android:background">@color/white</item> 
    </style> 

    <style name ="greenTitle"> 
     <item name="android:textColor">@color/white</item> 
     <item name ="android:background">@color/green</item> 
    </style> 

    <style name = "Theme" parent="@android:style/Theme"> 
     </style> 

    <style name = "Theme.Red" > 
     <item name="textTitle">@style/redTitle</item> 
     <item name="textBody">@style/redBody</item> 

     </style> 

    <style name = "Theme.Blue" > 
     <item name="textTitle">@style/blueTitle</item> 
     <item name="textBody">@style/blueBody</item> 

     </style> 
    <style name = "Theme.Green"> 
     <item name="textTitle">@style/greenTitle</item> 
     <item name="textBody">@style/greenBody</item> 

     </style> 

回答

1

尝试在SharedPreferences保存您的主题名称。

private SharedPreferences preferences; 

@Override 
protected void onCreate(Bundle state){ 
    super.onCreate(state); 
    . . . 

    // Restore preferences 
    preferences = getSharedPreferences("YourPrefsName", 0); 
    String theme = preferences.getString("THEME", false); 

    //set your theme. 
} 

public void onClick(View v) { 
    SharedPreferences.Editor preferencesEditor = preferences.edit(); 
    String bgColor = spinnerColor.getSelectedItem().toString(); 
    preferencesEditor.putString("THEME", bgColor); 
    preferencesEditor.commit(); 
} 
+0

我刚开始阅读共享偏好uo,因为我想应用主题的所有活动,但无法找到一个简单的教程。 – Calgar99

+0

检查我的编辑笔记;) –

+0

如果我选择红色,共享首选项是红色,但显示绿色? – Calgar99