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

Làm thế nào để có được nút Vòng tròn tùy chỉnh trong Android?

Ví dụ này trình bày cách tải nút Vòng tròn tùy chỉnh trong android.

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 / actiivity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:orientation = "vertical">
   <Button
      android:id = "@+id/text"
      android:textSize = "18sp"
      android:textAlignment = "center"
      android:background = "@drawable/round_button"
      android:layout_width = "150dp"
      android:textColor = "#000"
      android:layout_height = "150dp" />
</LinearLayout>

Trong đoạn mã trên, chúng ta đã sử dụng nút có nền round_button. vì vậy hãy tạo round_button.xml trong thư mục có thể vẽ và thêm mã sau.

<?xml version = "1.0" encoding = "utf-8"?>
<selector xmlns:android = "https://schemas.android.com/apk/res/android">
   <item android:state_pressed = "false">
      <shape android:shape = "oval">
         <solid android:color = "#ffff00"/>
      </shape>
   </item>
   <item android:state_pressed = "true">
      <shape android:shape = "oval">
         <solid android:color = "#fff000"/>
      </shape>
   </item>
</selector>

Trong đoạn mã trên, chúng tôi đã thực hiện hai trạng thái là nhấn nút trên và nút nhả. khi người dùng nhấp vào nút, nó sẽ chuyển sang trạng thái của nút và cung cấp hình dạng cho nút.

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

package com.example.andy.myapplication;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   Button button;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      button = findViewById(R.id.text);
      button.setText("Button");
   }
}

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 -

Làm thế nào để có được nút Vòng tròn tùy chỉnh trong Android?

Trong kết quả trên, nó hiển thị màn hình mặc định, bấm vào một nút, nó sẽ thay đổi màu sắc của một nút như hình dưới đây -

Làm thế nào để có được nút Vòng tròn tùy chỉnh trong Android?