2013-03-02 6 views
1

Bonjour à nouveau, j'ai une autre question au sujet de mon bonjour app monde Je veux changer l'arrière-plan quand un bouton est pressé alors je l'ai fait:fond application Android Couleur Bouton

public void onclick01(View View) 
     { 
      View.setBackgroundColor(Color.GREEN); 

     } 

Mais qui change la couleur d'arrière-plan le bouton et pas toute l'application.


Modifier

J'ai deux autres questions.

1) Comment pourrais-je mettre

View.setBackgroundColor(Color.GREEN); 

à quelque chose comme:

View.setBackgroundColor(Color.RANDOM); 

2) Comment pourrais-je faire la même chose pour changer la couleur du texte? quelque chose comme:

View.setTextColor(Color.GREEN);? 

Répondre

1

main_act.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/b1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button" /> 

</LinearLayout> 

activité

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
Button b1; 
LinearLayout layout; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_act); 
    layout=(LinearLayout)findViewById(R.id.layout); 
    blueButton=(Button)findViewById(R.id.b1); 
    b1.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     layout.setBackgroundColor(Color.BLUE); 

    } 
}); 
} 
} 
0

Si vous souhaitez définir par xml, alors vous devez faire comme ci-dessous:

android:background="@android:color/green" 

en cas si vous décidez d'utiliser le code couleur par défaut Android ou si vous avez des couleurs spécifié dans Colors.xml, puis utilisez

android:background="@colors/green" 

Si vous voulez faire un programme, puis faire:

 LinearLayout linearlayout=(LinearLayout) findViewById(R.layout.yourlayout); 
    linearlayout.setBackgroundColor(Color.GREEN); 
+0

que dois-je mettre pour (R.layout "YourLayout".) – Cnorwood7641

+0

id de votre mise en page .... android: id = "@ + id/yourlayout" ???? – Shiv

0

Ici Voir référence la vue de votre bouton. Vous devez donc créer un objet pour la mise en page de parent un puis

layout.setBackgroundColor(Color.GREEN); 
0
public void setActivityBackgroundColor(int color) { 
view = this.getWindow().getDecorView(); 
view.setBackgroundColor(color); 
} 

appeler ensuite de votre passage OnClickListener en tout ce que c olor tu veux.

+0

ce code me donne plusieurs messages d'erreur. – Cnorwood7641

0

Vous devriez avoir à utiliser xml pour cette situation

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 

<item android:state_pressed="true"> 
    <shape android:shape="rectangle"> 
     <solid android:color="yourColor"/> 
    </shape> 
</item> 

<item> 
    <shape android:shape="rectangle"> 
     <solid android:color="yourColor"/> 
    </shape> 
</item> 

</selector> 
0

Utilisez Selector vous obtiendrez une action bouton de presse.

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/button_background" android:state_pressed="false"/> 
<!-- default --> 
<item android:drawable="@drawable/button_pressed" android:state_pressed="true"/> 
<!-- pressed --> 
</selector>