Computer >> Máy Tính >  >> Lập trình >> Android

Cách tạo Hai hoạt động với thanh trạng thái có màu khác nhau trong Android.

Có rất nhiều tình huống, trong đó chúng ta cần thay đổi các màu thanh hành động khác nhau theo yêu cầu của đối tượng. Ví dụ này minh họa về cách tạo Hai hoạt động với thanh trạng thái có màu khác nhau.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:background = "#dde4dd"
   android:gravity = "center"
   android:orientation = "vertical">
   <Button
      android:id = "@+id/click"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:text = "click for second"/>
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã tạo nút on khi bạn nhấp vào nút, nó sẽ gọi hoạt động thứ hai.

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
   @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor("#FFFF00"));
         }
         setContentView(R.layout.activity_main);
         Button button = findViewById(R.id.click);
         button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent i = new Intent(MainActivity.this,Main2Activity.class);
               startActivity(i);
            }
         });
      }
   }
}

Để thay đổi mã vạch trạng thái, chúng tôi đã sử dụng setStatusBarColor () như hình dưới đây -

getWindow().setStatusBarColor(Color.parseColor("#FFFF00"));

Bước 4 - Thêm mã sau vào res / layout / activity_main2.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".Main2Activity">
</android.support.constraint.ConstraintLayout>

Bước 5 - Thêm mã sau vào src / MainActivity2.java

package com.example.andy.myapplication;

import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {
         getWindow().setStatusBarColor(Color.parseColor("#B22222"));
      }
      setContentView(R.layout.activity_main2);
   }
}

Bước 6 - Thêm mã sau vào tệp kê khai.java

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
   package = "com.example.andy.myapplication">
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity
         android:name = ".MainActivity"
         android:screenOrientation = "portrait">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <activity android:name = ".Main2Activity"></activity>
   </application>
</manifest>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Cách tạo Hai hoạt động với thanh trạng thái có màu khác nhau trong Android.

Trong kết quả trên, nó cho biết hoạt động đầu tiên với thanh trạng thái màu vàng, bây giờ nhấp vào nút, nó sẽ gọi hoạt động thứ hai với trạng thái màu đỏ như hình dưới đây -

Cách tạo Hai hoạt động với thanh trạng thái có màu khác nhau trong Android.