Schaltfläche – Hintergrundfarbe beim Klicken ändern
-
21-12-2019 - |
Frage
Ich habe 8 Schaltflächen in meiner Aktivität.Was ich suche, ist, dass die Schaltflächen einen Standardhintergrund haben und wenn auf eine Schaltfläche geklickt wird, sollte sich die Hintergrundfarbe in eine andere Farbe ändern.Dieser Teil ist ziemlich einfach.Wenn ich jedoch auf eine andere Schaltfläche klicke, sollte sich die Hintergrundfarbe der ersten Schaltfläche wieder auf die Standardfarbe ändern.Ich verstehe, dass dies mithilfe von „Selektorzuständen“ erfolgen wird, bin mir jedoch nicht ganz sicher, wie ich es implementieren soll.Je mehr ich darüber lese, desto verwirrter bin ich.
Im Moment ist die XML-Datei, die ich habe, wie folgt:im Zeichenordner
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" android:state_pressed="true"/>
<item android:drawable="@color/dark_grey" android:state_focused="true"/>
<item android:drawable="@drawable/image_border"/>
</selector>
Der drawable/image_border in der XML wird verwendet, um die Form für die Schaltfläche zu definieren.Unten ist die image_border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/dark_grey" />
<stroke
android:width="4dp"
android:color="@color/light_grey" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Kann mir jemand helfen, wie ich die XML-Datei so ändern kann, dass sie sich so verhält, wie ich es brauche?
[BEARBEITEN 1]
Alle folgenden Antworten deuten auf eine ähnliche Lösung hin.Hier sind die Änderungen, die ich vorgenommen habe.Aber wenn ich die Taste drücke, wechselt es trotzdem in die angegebene Farbe, kehrt aber sofort wieder zur Standardfarbe zurück.
button_background_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_border_blue" android:state_pressed="true"/>
<item android:drawable="@color/dark_grey" android:state_focused="true"/>
<item android:drawable="@drawable/image_border"/>
</selector>
image_border_blue.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/blue" />
<stroke
android:width="4dp"
android:color="@color/blue" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Irgendwelche Gedanken?
Lösung
Erstellen Sie eine Form mit dem Namen button_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/blue" />
<stroke
android:width="4dp"
android:color="@color/blue" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Angenommen, Sie haben zwei Schaltflächen, deren IDs lauten R.id.btn
Und R.id.btn1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 2" />
</LinearLayout>
Schreiben Sie die onClick()
Methode, mit der Sie die geänderte Farbe beibehalten können, bis eine andere Taste gedrückt wird.
Button button;
public void onClick(View v) {
Drawable dr = getResources().getDrawable(R.drawable.button_pressed);
dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP);
switch (v.getId()) {
case R.id.btn:
if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
case R.id.btn2:
if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
default:
break;
}
}
Ich denke, jetzt wirst du bekommen, was du tun wolltest.
Andere Tipps
Verwenden Sie diesen Selektor und legen Sie es in einen schädlichen Ordner und setzen Sie den Hintergrund der Schaltfläche auf diesen Selektor.
generasacodicetagpre.
Ich empfehle Ihnen diesen Selektor.
Erstellen Sie einfach eine einfache Datei „selector.xml“ im Zeichenordner und fügen Sie dann den Selektor zu Ihrer Schaltfläche hinzu android:background="@drawable/selector"
oder per Code wie diesem: yourButton.setBackground(getResources().getDrawable(R.drawable.selector));
selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@android:color/holo_blue_light" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@android:color/holo_green_dark" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="@android:color/holo_purple" />
<item
android:drawable="@drawable/yourdrawable" />
</selector>
Der erste Artikel ist für pressed
, der zweite für focused
und das letzte ist für selected
Zustand.
changing background color in layout when respective color button is clicked in android
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b056ff"
android:id="@+id/l1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:layout_gravity="center"
android:text="RED"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:layout_gravity="center"
android:text="GREEN" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:layout_gravity="center"
android:text="BLUE"/>
MyActivity.java
package ram.android.com.cwp1;
import android.app.Activity;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
/**
* Created by VENKATESH on 10-Jun-16.
*/
public class MyActivity extends Activity implements View.OnClickListener {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
Button r, g, b;
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ll = (LinearLayout) findViewById(R.id.l1);
r = (Button) findViewById(R.id.b1);
g = (Button) findViewById(R.id.b2);
b = (Button) findViewById(R.id.b3);
r.setOnClickListener(this);
g.setOnClickListener(this);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
ll.setBackgroundColor(Color.RED);
break;
case R.id.b2:
ll.setBackgroundColor(Color.GREEN);
break;
case R.id.b3:
ll.setBackgroundColor(Color.BLUE);
break;
}
}
}
I know it is very late but I hope some one would find useful. I have very simple solution, I have used in my app and works awesome.
Let me explain the logic, 1. Keep track of 2 button clicks - previous button clicked and current button clicked. I am using ArrayList 2. for every button click keep updating the previous and current button clicks value in ArrayList. 3. change the background color of previous button clicked. 4. change the background color of current button clicked.
I hope the logic is simple and straightforward.
Here is the implementation
xml
<Button
android:onClick="onClickRosterDay"
android:text="Mon"
android:textColor="@color/textColorWhite"
android:background="@color/colorAccent"
android:id="@+id/rosterMonday"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
the OnClick method is defined for every button in XML, in the example it is onClickRosterDay
Java
//buttons
Button rosterMonday;
Button rosterTuesday;
Button rosterWednesday;
Button rosterThursday;
Button rosterFriday;
Button rosterSaturday;
//ArrayList to track button clicks
private ArrayList<Button> buttonClickedDay;
in OnCreate
buttonClickedDay = new ArrayList<>();
// to start with these are the default clicks.
buttonClickedDay.add(rosterMonday ); //previous button clicked
buttonClickedDay.add(rosterMonday ); // current button clicked
public void onClickRosterDay(View v) {
switch (v.getId()){
case R.id.rosterMonday:
daySelected = "MONDAY";
// move current click button to previous button clicked position
buttonClickedDay.set(0, buttonClickedDay.get(1));
// update current clicked position
buttonClickedDay.set(1,rosterMonday);
break;
case R.id.rosterTuesday:
daySelected = "TUESDAY";
buttonClickedDay.set(0, buttonClickedDay.get(1));
buttonClickedDay.set(1,rosterTuesday);
break;
case R.id.rosterWednesday:
daySelected = "WEDNESDAY";
buttonClickedDay.set(0, buttonClickedDay.get(1));
buttonClickedDay.set(1,rosterWednesday);
break;
case R.id.rosterThursday:
daySelected = "THURSDAY";
buttonClickedDay.set(0, buttonClickedDay.get(1));
buttonClickedDay.set(1,rosterThursday);
break;
case R.id.rosterFriday:
daySelected = "FRIDAY";
buttonClickedDay.set(0, buttonClickedDay.get(1));
buttonClickedDay.set(1,rosterFriday);
break;
case R.id.rosterSaturday:
daySelected = "SATURDAY";
buttonClickedDay.set(0, buttonClickedDay.get(1));
buttonClickedDay.set(1,rosterSaturday);
break;
}
if(buttonClickedDay.get(0) != buttonClickedDay.get(1)) {
// update background color of previous button clicked
buttonClickedDay.get(0).setBackgroundColor(this.getResources().getColor(R.color.colorAccent));
// update background color of current button clicked
buttonClickedDay.get(1).setBackgroundColor(this.getResources().getColor(R.color.textBackgroundGreen));
}
}