Hallo und danke für eure Hilfe!
Ich habe Fortschritte gemacht.
In einer Activity habe ich Buttons erstellt, welche eine andere Activity öffnen und automatisch die Hintergrundfarbe ändern.
(Heißt also, dass ich das eigentliche Problem schon gelöst habe.)
Allerdings gibt's noch ein paar kleine Unklarheiten, die wir evt. gemeinsam lösen können.
In meiner MainActivity.java habe ich folgenden Code:
Code:
package com.example.USER.startanotheractivity;
import android.content.Intent;
import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity
{
public static final String EXTRA_MESSAGE = "com.example.USER.startanotheractivity";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeColorBrown(View view)
{
Intent intentColor = new Intent(MainActivity.this, DisplayMessageActivity.class);
int backgroundColor = R.color.brown;
intentColor.putExtra("backgroundColor", backgroundColor);
startActivity(intentColor);
}
public void changeColorBlue(View view)
{
Intent intentColor = new Intent(MainActivity.this, DisplayMessageActivity.class);
int backgroundColor = R.color.blue;
intentColor.putExtra("backgroundColor", backgroundColor);
startActivity(intentColor);
}
public void changeColorRed(View view)
{
Intent intentColor = new Intent(MainActivity.this, DisplayMessageActivity.class);
int backgroundColor = R.color.red;
intentColor.putExtra("backgroundColor", backgroundColor);
startActivity(intentColor);
}
public void switchActivity(View view)
{
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
startActivity(intent);
}
}
In der activity_main.xml sieht es folgendermaßen aus:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/con_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.USER.startanotheractivity.MainActivity">
<Button
android:id="@+id/button_change_color_brown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/button_change_color_brown"
android:onClick="changeColorBrown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/button_change_color_brown" />
<Button
android:id="@+id/button_change_color_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:onClick="changeColorBlue"
android:text="@string/button_change_color_blue"
tools:text="@string/button_change_color_blue"
app:layout_constraintBottom_toTopOf="@+id/button_change_color_brown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_change_color_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:onClick="changeColorRed"
android:text="@string/button_change_color_red"
app:layout_constraintBottom_toTopOf="@+id/button_change_color_brown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_change_color_blue" />
<Button
android:id="@+id/button_start_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="16dp"
android:onClick="switchActivity"
android:text="@string/button_start_activity"
app:layout_constraintBottom_toTopOf="@+id/button_change_color_blue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Die zweite Activity heißt DisplayMessageActivity.java und verfügt über folgenden Code:
Code:
package com.example.USER.startanotheractivity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
int backgroundColor = getIntent().getIntExtra("backgroundColor", 0);
ConstraintLayout con_layout_second_activity = (ConstraintLayout) findViewById(R.id.con_layout_second_activity);
con_layout_second_activity.setBackgroundResource(backgroundColor);
}
}
Unklarheiten, die ich aktuell habe:
1) Codes zusammenfassen
Aus AutoHotkey weiß ich, dass es sinnvoll ist, gleiche Programmcodezeilen zusammenzufassen.
Z.B. habe ich 3x diesen Programmblock in der MainActivity.java:
Code:
Intent intentColor = new Intent(MainActivity.this, DisplayMessageActivity.class);
int backgroundColor = R.color.blue;
intentColor.putExtra("backgroundColor", backgroundColor);
startActivity(intentColor);
Der Unterschied ist nur der Farbwert - alles andere ist gleich.
In AutohotKey würde ich statt des Farbwerts eine Variable nehmen und beim Aufruf der Funktion die Variable definieren.
Hab leider keine Ahnung, ob oder wie das hier geht.
2) Text
Auch wenn es kein echtes Problem ist, wundert es mich, warum die Button-Beschriftungen alle in Großbuchstaben sind.
Statt Blue, Red, Brown und Start Activity steht:
BLUE, RED, BROWN und START ACTIVITY.
3) Speichern der Hintergrundfarbe
Sobald ich auf einen Farb-Button klicke, öffnet sich die zweite Activity und die Hintergrundfarbe wurde korrekt festgelegt.
Gehe ich zurück und klicke auf den Button Start Activity, ist die Hintergrundfarbe wieder weiß.
Warum?
Ich dachte, die bundles speichern den Zustand der Activity.
Achso und public static final String EXTRA_MESSAGE = "com.example.USER.startanotheractivity"; brauche ich evt. gar nicht mehr.
Danke nochmals für euren Support!
P.S. Werde jetzt vorerst nur noch in diesem Forum posten.