Public class SvetoforActivity extends AppCompatActivity { private CardView redLight


Download 17.66 Kb.
Sana17.06.2023
Hajmi17.66 Kb.
#1522892
Bog'liq
VohidovFayzullo


public class SvetoforActivity extends AppCompatActivity {

private CardView redLight;


private CardView yellowLight;
private CardView greenLight;

private int redTime;


private int redYellowTime;
private int greenTime;

Handler handler;


private int yellowTime;

@SuppressLint("MissingInflatedId")


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_svetofor);
Intent intent = getIntent();

handler = new Handler(Looper.getMainLooper());

redTime = intent.getIntExtra("RED", 3) * 1000;
redYellowTime = intent.getIntExtra("RED_YELLOW", 3) * 1000;
greenTime = intent.getIntExtra("GREEN", 3) * 1000;
yellowTime = intent.getIntExtra("YELLOW", 3) * 1000;

redLight = findViewById(R.id.circleRed);


yellowLight = findViewById(R.id.circleYellow);
greenLight = findViewById(R.id.circleGreen);
handler.postDelayed(this::startTrafficLight, 1000);

}

private void startTrafficLight() {



changeLightColor(redLight, Color.RED);
changeLightColor(yellowLight, Color.GRAY);
changeLightColor(greenLight, Color.GRAY);

handler.postDelayed(() -> {


changeLightColor(redLight, Color.RED);


changeLightColor(yellowLight, Color.YELLOW);
changeLightColor(greenLight, Color.GRAY);

handler.postDelayed(() -> {


changeLightColor(redLight, Color.GRAY);
changeLightColor(yellowLight, Color.GRAY);
changeLightColor(greenLight, Color.GREEN);

handler.postDelayed(() -> {


changeLightColor(redLight, Color.GRAY);
changeLightColor(yellowLight, Color.YELLOW);
changeLightColor(greenLight, Color.GRAY);

handler.postDelayed(() -> {


changeLightColor(redLight, Color.RED);
changeLightColor(yellowLight, Color.GRAY);
changeLightColor(greenLight, Color.GRAY);
startTrafficLight();
}, yellowTime);

}, greenTime);

}, redYellowTime);

}, redTime);


}

private void changeLightColor(CardView light, int color) {


light.setCardBackgroundColor(color);
}

}


public class MenuActivity extends AppCompatActivity {

@Override


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
EditText redLight = findViewById(R.id.inputRedLight);
EditText redYellowLight = findViewById(R.id.inputRedYellowLigth);
EditText greenLight = findViewById(R.id.inputGreenLight);
EditText yellowLight = findViewById(R.id.inputYellowLigth);

Intent intent = new Intent(this, SvetoforActivity.class);


intent.putExtra("RED", getValue(redLight.getText().toString()));
intent.putExtra("RED_YELLOW", getValue(redYellowLight.getText().toString()));
intent.putExtra("GREEN", getValue(greenLight.getText().toString()));
intent.putExtra("YELLOW", getValue(yellowLight.getText().toString()));

findViewById(R.id.startButton).setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
startActivity(intent);
}
});
}

private int getValue(String value) {


int number = 3;
if (value.equals("") || value.equals("0")) {
return number;
}
return Integer.parseInt(value);
}
}


xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuActivity">

android:id="@+id/constraint"
android:layout_width="60dp"
android:layout_height="185dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1">

android:id="@+id/circleRed"
android:layout_width="50dp"
android:layout_height="50dp"
app:cardBackgroundColor="@color/red"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleYellow"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

android:id="@+id/circleYellow"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginVertical="4dp"
app:cardBackgroundColor="#888888"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleGreen"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleRed" />

android:id="@+id/circleGreen"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="4dp"
app:cardBackgroundColor="#888888"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleYellow" />



android:id="@+id/inputRedLight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="@id/constraint"
app:layout_constraintRight_toRightOf="@id/constraint"
app:layout_constraintTop_toBottomOf="@id/constraint" />

android:id="@+id/constraint2"
android:layout_width="60dp"
android:layout_height="185dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1">

android:id="@+id/circleRed2"
android:layout_width="50dp"
android:layout_height="50dp"
app:cardBackgroundColor="@color/red"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleYellow2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

android:id="@+id/circleYellow2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginVertical="4dp"
app:cardBackgroundColor="@color/yellow"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleGreen2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleRed2" />

android:id="@+id/circleGreen2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="4dp"
app:cardBackgroundColor="#888888"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleYellow2" />



android:id="@+id/inputRedYellowLigth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="@id/constraint2"
app:layout_constraintRight_toRightOf="@id/constraint2"
app:layout_constraintTop_toBottomOf="@id/constraint2" />

android:id="@+id/constraint3"
android:layout_width="60dp"
android:layout_height="185dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7">

android:id="@+id/circleRed3"
android:layout_width="50dp"
android:layout_height="50dp"
app:cardBackgroundColor="@color/gray"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleYellow3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

android:id="@+id/circleYellow3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginVertical="4dp"
app:cardBackgroundColor="#888888"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleGreen3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleRed3" />

android:id="@+id/circleGreen3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="4dp"
app:cardBackgroundColor="@color/green"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleYellow3" />



android:id="@+id/inputGreenLight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="@id/constraint3"
app:layout_constraintRight_toRightOf="@id/constraint3"
app:layout_constraintTop_toBottomOf="@id/constraint3" />

android:id="@+id/constraint4"
android:layout_width="55dp"
android:layout_height="183dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7">

android:id="@+id/circleRed4"
android:layout_width="50dp"
android:layout_height="50dp"
app:cardBackgroundColor="@color/gray"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleYellow4"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

android:id="@+id/circleYellow4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginVertical="4dp"
app:cardBackgroundColor="@color/yellow"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="@id/circleGreen4"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleRed4" />

android:id="@+id/circleGreen4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="4dp"
app:cardBackgroundColor="#888888"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/circleYellow4" />



android:id="@+id/inputYellowLigth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="@id/constraint4"
app:layout_constraintRight_toRightOf="@id/constraint4"
app:layout_constraintTop_toBottomOf="@id/constraint4" />


Download 17.66 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling