Add DEV4.5/exo4_controle_2022
This commit is contained in:
112
DEV4.5/exo4_controle_2022
Normal file
112
DEV4.5/exo4_controle_2022
Normal file
@@ -0,0 +1,112 @@
|
||||
Fichier MainColorActivity.java
|
||||
|
||||
package com.example.organisation;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
public class MainColorActivity extends AppCompatActivity {
|
||||
private View selectedView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main_color);
|
||||
|
||||
findViewById(R.id.blueBox).setOnClickListener(v -> openColorPicker(v));
|
||||
findViewById(R.id.greenBox).setOnClickListener(v -> openColorPicker(v));
|
||||
}
|
||||
|
||||
private void openColorPicker(View view) {
|
||||
selectedView = view;
|
||||
Intent intent = new Intent(this, ColorPickerActivity.class);
|
||||
startActivityForResult(intent, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
|
||||
int color = data.getIntExtra("selectedColor", 0);
|
||||
selectedView.setBackgroundColor(color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Fichier ColorPickerActivity.java
|
||||
|
||||
package com.example.organisation;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
public class ColorPickerActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_color_picker);
|
||||
}
|
||||
|
||||
public void selectColor(View view) {
|
||||
int color = ((View) view).getSolidColor();
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra("selectedColor", color);
|
||||
setResult(Activity.RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
Fichier activity_main_color.xml
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="20dp">
|
||||
|
||||
<View
|
||||
android:id="@+id/blueBox"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="#33B5E5"
|
||||
android:onClick="openColorPicker"/>
|
||||
|
||||
<View
|
||||
android:id="@+id/greenBox"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="#99CC00"
|
||||
android:onClick="openColorPicker"/>
|
||||
</LinearLayout>
|
||||
|
||||
Fichier activity_color_picker.xml
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal"
|
||||
android:padding="20dp">
|
||||
|
||||
<View
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="#FF0000"
|
||||
android:onClick="selectColor"/>
|
||||
|
||||
<View
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="#00FF00"
|
||||
android:onClick="selectColor"/>
|
||||
|
||||
<View
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:background="#0000FF"
|
||||
android:onClick="selectColor"/>
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user