2014-10-13 3 views
1

Android 앱에서 3 개의 라디오 버튼을 사용하여 사용자가 색상을 변경할 수있는 활동의 배경색을 변경하려고합니다. sharedPreferences를 사용하여이 작업을 수행하고 있습니다.Android : SharedPreferences를 사용하여 특정 활동의 배경색 변경

그래서 라디오 페이지에서 색상을 선택하고 sharedPrefernces를 사용하는 코드가 다음과 같은 활동 페이지가 있습니다. (색상이 바뀌어야 할 때 토스트 메시지가 표시되기 때문에 switch 문도 작동합니다.)는 XML이

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.practical3_10327751_donnacha_holmes.Preferences" > 

<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/radiogroup" 
    > 

    <RadioButton 
     android:id="@+id/radioButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Green" /> 

    <RadioButton 
     android:id="@+id/radioButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Yellow" /> 

    <RadioButton 
     android:id="@+id/radioButton3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Blue" /> 

</RadioGroup> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Back" /> 

과 같은

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_preferences); 
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup); 
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE); 
      SharedPreferences.Editor editor = prefs.edit(); 

      String colourSelected = ""; 
      switch (checkedId) { 
       case R.id.radioButton1 : 
        colourSelected = "YELLOW"; 
        editor.putString("colour", colourSelected); 
        editor.commit(); 
        break; 
       case R.id.radioButton2 : 
        colourSelected = "YELLOW"; 
        editor.putString("colour", colourSelected); 
        editor.commit(); 
        break; 

       case R.id.radioButton3 : 
        colourSelected = "BLUE"; 
        editor.putString("colour", colourSelected); 
        editor.commit(); 
        break; 
      } 
     } 
    }); 
} 

는 그 다음 backgrou 하을 변경하는 활동이

색상을 변경하려고 할 때 다음과 같다 차 색상 :이 빨간색으로 나는이 페이지로 이동 때마다 설정되고 있기 때문에

public class Activity2 extends ActionBarActivity { 

RelativeLayout rl; 
String colour; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_activity2); 

    SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE); 
    colour = prefs.getString("Colour", "WHITE"); 

    rl = (RelativeLayout)findViewById(R.id.RelativeLayout); 
    if(colour=="GREEN"){ 
    rl.setBackgroundColor(Color.GREEN); 
    } 
    else if(colour=="YELLOW"){ 
     rl.setBackgroundColor(Color.YELLOW); 
    } 
    else if(colour=="BLUE"){ 
     rl.setBackgroundColor(Color.BLUE); 
    } 
    else{ 
     rl.setBackgroundColor(Color.RED); 
    } 

나는 배경 색상이 변경되고 있음을 알고있다. 도움 주셔서 감사합니다!

답변

3

연산자 == 연산자는 두 문자열이 완전히 동일한 개체인지 확인합니다.

.equals() 메서드는 두 문자열의 값이 같은지 확인합니다.

따라서 문자열을 비교하려면 .equals()을 사용하십시오.

if(colour.equals("GREEN")){ 
    rl.setBackgroundColor(Color.GREEN); 
    } 
    else if(colour.equals("YELLOW")){ 
     rl.setBackgroundColor(Color.YELLOW); 
    } 
    else if(colour.equals("BLUE")){ 
     rl.setBackgroundColor(Color.BLUE); 
    } 
    else{ 
     rl.setBackgroundColor(Color.RED); 
    } 

같은 비교를 다시 작성하면 색상로 값을 저장하고 컬러으로 검색하려고, 그래서 답장을

colour = prefs.getString("Colour", "WHITE"); 

colour = prefs.getString("colour", "WHITE"); 
+0

헤이 감사 변경 ! 나는 당신이 의미하는 것을 얻지 만 작동하지 않습니다. 당신이 sharedpreferences에 색상을 저장하는 방법이 될 수 있다고 생각합니까? – Donnacha

+0

@Donnacha 위의 변경 후에 프로젝트를 지우셨습니까? 프로젝트를 지우고 다시 시도하십시오. –

+0

그래, 아직 운이 .. – Donnacha

관련 문제