Ändern Sie Hintergrundfarbe mit Radio-Buttons Android
-
12-10-2019 - |
Frage
Ich versuche, den Hintergrund einer Registerkarte meiner Anwendung zu ändern, indem Sie einen Radio-Button aus einem Radiogroup Auswahl, aber ich bin nicht sicher, wie um dies zu realisieren.
Bisher habe ich Favs.java:
import android.app.Activity;
import android.os.Bundle;
public class Favs extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favs);
}
}
die Punkte auf diese XML-Datei -> favs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favorite color?"
android:padding="3dip"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:onClick="onClick" />
<RadioButton android:id="@+id/radio_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
</RadioGroup>
</LinearLayout>
Ich verstehe, dass die Android-Version: onClick = needs „onClick“ gibt es jedoch sein, ich habe keine Ahnung, was danach zu tun
.Lösung
hallo Verwendung unter Code zum Ändern des Hintergrunds nach Optionsfeld Auswahl
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/LinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favorite color?"
android:padding="3dip"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Group1"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
/>
<RadioButton android:id="@+id/radio_yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
</RadioGroup>
</LinearLayout>
Aktivität
public class Change extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout ll=(LinearLayout) findViewById(R.id.LinearLayout);
final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
final RadioButton radio_yellow = (RadioButton) findViewById(R.id.radio_yellow);
radio_red.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ll.setBackgroundColor(Color.RED);
}
});
radio_yellow.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ll.setBackgroundColor(Color.YELLOW);
}
});
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow