2017-06-15 4 views
0

Hallo Ich habe eine benutzerdefinierte Runde Ecke Taste (gps_button), die funktioniert, aber ich würde gerne die Farbe der Schaltfläche abhängig von ihrem Zustand ändern, aber, die Farbe ändert sich nicht. Farbvariablen werden in Colors.xml gespeichertFarbe ändern oder benutzerdefinierte Taste

Meine Hauptfrage ist, wie die Farbe von Color.xml auf meine benutzerdefinierte Schaltfläche eingestellt werden?

dies ist mein Code Hauptaktivität:

public void start_gps (View view){ 

gps_button = (Button) (findViewById(R.id.start_gps_button)); 

if ((log_state == true) && (start_gps_button_state == false)){ 

      start_gps_button_state = true; 
      gps_button.setBackgroundColor(R.color.startGpsColor); 
      gps_button.setText("STOP"); 
      voyagePoints.setText("0"); 

      Toast.makeText(this, "GPS Started", Toast.LENGTH_SHORT).show(); 

     }else if ((log_state == true) && start_gps_button_state == true){ 

      start_gps_button_state = false; 
      gps_button.setBackgroundColor(R.color.stopGpsColor); 
      gps_button.setText("START"); 

      Toast.makeText(this, "GPS Stopped", Toast.LENGTH_SHORT).show(); 
     } 
    } 

Dies ist die benutzerdefinierte Schaltfläche:

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="true" > 
     <shape android:shape="rectangle" > 
      <corners android:radius="10dip" /> 
      <stroke android:width="1dip" android:color="#1EB7F1" /> 
      <gradient android:angle="-90" android:startColor="#1EB7F1" android:endColor="#1EB7F1" /> 
     </shape> 
    </item> 
    <item android:state_focused="true"> 
     <shape android:shape="rectangle" > 
      <corners android:radius="10dip" /> 
      <stroke android:width="1dip" android:color="#1EB7F1" /> 
      <solid android:color="#1EB7F1"/> 
     </shape> 
    </item> 
    <item > 
     <shape android:shape="rectangle" > 
      <corners android:radius="10dip" /> 
      <stroke android:width="1dip" android:color="#1EB7F1" /> 
      <gradient android:angle="-90" android:startColor="#1EB7F1" android:endColor="#1EB7F1" /> 
     </shape> 
    </item> 
</selector> 

Dies ist die Color.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="colorPrimary">#A7E483</color> 
    <color name="colorPrimaryDark">#A7E483</color> 
    <color name="colorAccent">#FF4081</color> 
    <color name="stopGpsColor">#F9108E</color> 
    <color name="startGpsColor">#1EB7F1</color> 
</resources> 

Antwort

0

this question See. Sie haben button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

+0

Dank aber, dass für die „Fabrik“ Taste anwendbar sieht, wird es> für meine custome Taste funktioniert? – Cardona

+1

ja wird es. benutze einfach gps_button.setBackgroundColor (getResources(). getColor (R.color.colorPrimary)); –

+0

@Cardona Alle Schaltflächen, einschließlich Ihrer benutzerdefinierten Schaltfläche, haben Zugriff auf dieselbe setBackgroundColor() -Methode –

Verwandte Themen