Public class SvetoforActivity extends AppCompatActivity { private CardView redLight
Download 17.66 Kb.
|
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;
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:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MenuActivity"> 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: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: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: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: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: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: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: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: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: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: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: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: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: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: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: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: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: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: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: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" /> android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.95" /> Download 17.66 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling