2017-04-18 222 views
-6

我在活动中有两个单选按钮。第一个按钮是名称默认和第二个绿色。如果我点击按钮绿色,我希望所有活动中的所有文本视图都具有背景颜色绿色,如果我点击按钮默认值,所有文字浏览将回到默认状态。文本视图,更改背景颜色

代码;

{ 



    super.onCreate(savedInstanceState); 

     setContentView(R.layout.settings2); 



    SharedPreferences preferences = 
    getSharedPreferences("AppPrefs" , 
    MODE_PRIVATE); 
    final SharedPreferences. Editor. 
     PrefsEditor = preferences.edit(); 
    final RelativeLayout root_layout= 
     (RelativeLayout) 
    findViewById(R.id.root_layout); 

    final TextView tv_text1 = (TextView) 
    findViewById(R.id.hym1t); 
    final TextView tv_text2 = (TextView)  
    findViewById(R.id.hym2t); 
    RadioGroup radioDate = ( 
     RadioGroup)  
     findViewById(R.id.radioGroup); 
    final RadioButton radio1 = ( 
     RadioButton) 
     findViewById(R.id.radio1); 
    RadioButton radio2 = (RadioButton)  
     findViewById(R.id.radio2); 


     radioDate.setOnCheckedChange 
     Listener 
    (new 
     RadioGroup.OnCheckedChange 
     Listener() 
     { 
      @Override 
      public void onCheckedChanged 
      (RadioGroup group, int.  
     checkedId) { 
       if (radio1.isChecked()) { 
     //set value 
        PrefsEditor.putString 
        ("RadioButtonSelected" , "1"  
    ); 


    findViews(mContext,root_layout, 1);//  
     root_layout is your xml root layout id 
       } 
       else   { 
    //set value 
        PrefsEditor.putString 
        ("RadioButtonSelected" , "2"  
    ); 


    findViews(mContext,root_layout, 2); 
    // root_layout is your xml root layout  
    id 
       } 
      } 
     }); 
    /*get value:*/ 
    String radioButtonSelected= 
    preferences.getStrin g 
    ("RadioButtonSelected" , "");} 



     public static void findViews(Context  
     context, View v, int flag){ 
     try { 
     if (v instanceof ViewGroup) { 
      ViewGroup vg = (ViewGroup) v; 
      for (int i = 0; i <    
     vg.getChildCount(); i++) { 
       View child = vg.getChildAt(i); 
    //you can recursively call this method 
       findViews(context, child); 
      } 
     } else if (v instanceof TextView) { 
     //do whatever you want ... 
      if (flag== 1) 
      { 
       v.setBackgroundResource 

     (R.drawable.favoff); 

      } 
      else { 
       v.setBackgroundResource 

       (R.drawable.favon); 
       } 
     } 
     } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    private static void findViews(Context   
    context, View child) 
    { 
    // TODO: Implement this method 
    } 



    } 
+0

你做了什么至今? – AbhayBohra

+0

这可能会帮助你:http://stackoverflow.com/questions/22044788/button-change-background-color-on-click –

回答

0

试试这个,

private final Context mContext = MyActivity.this; 

SharedPreferences preferences = getSharedPreferences("AppPrefs", MODE_PRIVATE); 
SharedPreferences.Editor PrefsEditor = preferences.edit(); 
RelativeLayout root_layout= (RelativeLayout) findViewById(R.id.root_layout); 
TextView tv_text1 = (TextView) findViewById(R.id.tv_text1); 
TextView tv_text2 = (TextView) findViewById(R.id.tv_text2); 

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 
RadioButton radio1 = (RadioButton) findViewById(R.id.radio1); 
RadioButton radio2 = (RadioButton) findViewById(R.id.radio2); 

radioDate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     if (radio1.isChecked()) { 
      //set value 
      PrefsEditor.putString("RadioButtonSelected", "1"); 

      findViews(mContext,root_layout,1);//root_layout is your xml root layout id 
     } 
     else 
     { 
      //set value 
      PrefsEditor.putString("RadioButtonSelected", "2"); 

      findViews(mContext,root_layout,2);//root_layout is your xml root layout id 
     } 
    } 
}); 


/*get value:*/ 
String radioButtonSelected=preferences.getString("RadioButtonSelected", ""); 

设置背景色:

public static void findViews(Context context, View v,int flag){ 
try { 
     if (v instanceof ViewGroup) { 
      ViewGroup vg = (ViewGroup) v; 
      for (int i = 0; i < vg.getChildCount(); i++) { 
       View child = vg.getChildAt(i); 
       //you can recursively call this method 
       findViews(context, child) 
      } 
     } else if (v instanceof TextView) { 
      //do whatever you want ... 
      if(flag==1) 
      { 
       v.setBackgroundResource(R.color.blue);//set default color 
      } 
      else{ 
       v.setBackgroundResource(R.color.green);//set green color 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

谢谢,但我想改变大约20个活动的文字背景,也是我会如果你能告诉我如何保持共享偏好的状态,我会很感激。 –

+0

感谢但只有一件事,我如何分配根布局Id –

+0

设置ID root_layout到您的邮件布局和使用 – user2025187