avatar
Untitled

Guest 189 2nd Mar, 2022

JAVA 8.37 KB
                                           
                         package com.example.oliwkacodingrgb;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    int r,g,b;
    View kwadracik;
    EditText wypisz1, wypisz2, wypisz3;
    SeekBar seekbar1, seekbar2, seekbar3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         kwadracik = findViewById(R.id.kwadracik);

        wypisz1 = findViewById(R.id.test);
        seekbar1 = (SeekBar) findViewById(R.id.seekBar1);
        seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekbar1) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekbar1) {
            }

            @Override
            public void onProgressChanged(SeekBar seekbar1, int progress,boolean fromUser) {
                // TODO Auto-generated method stub
                r=progress;
                wypisz1.setText(String.valueOf(r));
                kwadracik.setBackgroundColor(Color.rgb(r, g, b));
            }
        });

        wypisz2 = findViewById(R.id.test2);
        seekbar2 = (SeekBar) findViewById(R.id.seekBar2);
        seekbar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekbar2) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekbar2) {
            }

            @Override
            public void onProgressChanged(SeekBar seekbar2, int progress,boolean fromUser) {
                g=progress;
                wypisz2.setText(String.valueOf(g));
                kwadracik.setBackgroundColor(Color.rgb(r, g, b));
            }
        });

        wypisz3 = findViewById(R.id.test3);
        seekbar3 = (SeekBar) findViewById(R.id.seekBar3);
        seekbar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekbar3) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekbar3) {
            }

            @Override
            public void onProgressChanged(SeekBar seekbar3, int progress,boolean fromUser) {
                b=progress;
                wypisz3.setText(String.valueOf(b));
                kwadracik.setBackgroundColor(Color.rgb(r, g, b));
            }
        });

        wypisz1.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String value= wypisz1.getText().toString();
                int finalValue=Integer.parseInt(value);
                seekbar1.setProgress(finalValue);
            }
        });
        wypisz2.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String value= wypisz2.getText().toString();
                int finalValue=Integer.parseInt(value);
                seekbar2.setProgress(finalValue);
            }
        });
        wypisz3.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String value= wypisz3.getText().toString();
                int finalValue=Integer.parseInt(value);
                seekbar3.setProgress(finalValue);
            }
        });

}

    public void losowanie(View view) {
        Random ran = new Random();
        int r = ran.nextInt(256);
        int g = ran.nextInt(256);
        int b = ran.nextInt(256);
        seekbar1.setProgress(r);
        seekbar2.setProgress(g);
        seekbar3.setProgress(b);
        kwadracik.setBackgroundColor(Color.rgb(r, g, b));
    }
    }


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <View
        android:id="@+id/kwadracik"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:layout_below="@+id/kwadracik"
        android:layout_marginLeft="5dp"
        android:max="250" />

    <EditText
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignBottom="@+id/seekBar1"
        android:layout_marginBottom="0dp"
        android:layout_toRightOf="@+id/seekBar1"
        android:text="0"
        android:inputType="number" />

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:layout_below="@+id/seekBar1"
        android:layout_marginLeft="5dp"
        android:max="250" />

    <EditText
        android:id="@+id/test2"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignBottom="@+id/seekBar2"
        android:layout_marginBottom="0dp"
        android:layout_toRightOf="@+id/seekBar2"
        android:text="0"
        android:inputType="number" />

    <SeekBar
        android:id="@+id/seekBar3"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:layout_below="@+id/seekBar2"
        android:layout_marginLeft="5dp"
        android:max="250" />

    <EditText
        android:inputType="number"
        android:id="@+id/test3"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignBottom="@+id/seekBar3"
        android:layout_marginBottom="0dp"
        android:layout_toRightOf="@+id/seekBar3"
        android:text="0" />


    <Button
        android:onClick="losowanie"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:text="wylosuj" />
</RelativeLayout>
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM