如何在 Android 中實現按鈕表單部件?


此示例演示瞭如何實現 Android 按鈕表單部件。

步驟 1 - 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟 2 - 將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   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="MainActivity">
   <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <android.support.v7.widget.Toolbar
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="?attr/actionBarSize"
         android:background="?attr/colorPrimary"
         app:popupTheme="@style/Widget.Support.CoordinatorLayout" />
   </android.support.design.widget.AppBarLayout>
   <include layout="@layout/content" />
   <include layout="@layout/bottomsheet" />
</android.support.design.widget.CoordinatorLayout>

步驟 3 - 開啟 build.gradle (Module:app) 並新增以下依賴項

implementation 'com.android.support:design:28.0.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknifecompiler:8.8.1'

步驟 4 - 建立一個佈局 (bottomsheet.xml) 並新增以下程式碼 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/bottomSheet"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:padding="4dp"
   app:behavior_hideable="true"
   app:behavior_peekHeight="10dp"
   app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_gravity="center_vertical"
      android:weightSum="3">
      <TextView
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         android:text="Order Details"
         android:textColor="#444"
         android:textSize="18dp"
         android:textStyle="bold" />
      <TextView
         android:layout_width="0dp"
         android:gravity="right"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:textStyle="bold"
         android:textSize="15dp"
         android:text="₹435.00"></TextView>
   </LinearLayout>
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Chicken BBQ" />
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Chicken Biriyani" />
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Delivery Address"
      android:textColor="#444"
      android:textStyle="bold" />
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Flat No 404, India, Chennai" />
   <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="30dp"
      android:background="#000"
      android:text="PROCEED PAYMENT"
      android:textColor="#fff" />
</LinearLayout>

步驟 5 - 建立一個佈局 (content.xml) 並新增以下程式碼 -

<?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"
   android:orientation="vertical"
   tools:context="MainActivity"
   tools:showIn="@layout/activity_main">
   <Button
      android:id="@+id/btnBottomSheet"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Show Bottom Sheet"
      android:layout_centerInParent="true"/>
</RelativeLayout>

步驟 6 - 將以下程式碼新增到 src/MainActivity.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
   @BindView(R.id.btnBottomSheet)
   Button btnBottomSheet;
   @BindView(R.id.bottomSheet)
   LinearLayout layoutBottomSheet;
   BottomSheetBehavior sheetBehavior;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ButterKnife.bind(this);
      Toolbar toolbar = findViewById(R.id.toolbar);
      //setSupportActionBar(toolbar);
      sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);
      sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
         @Override
         public void onStateChanged(@NonNull View bottomSheet, int newState) {
            switch (newState) {
               case BottomSheetBehavior.STATE_HIDDEN:
               break;
               case BottomSheetBehavior.STATE_EXPANDED: {
                  btnBottomSheet.setText("Close Sheet");
               }
               break;
               case BottomSheetBehavior.STATE_COLLAPSED: {
                  btnBottomSheet.setText("Expand Sheet");
               }
               break;
               case BottomSheetBehavior.STATE_DRAGGING:
               break;
               case BottomSheetBehavior.STATE_SETTLING:
               break;
            }
         }
         @Override
         public void onSlide(@NonNull View bottomSheet, float slideOffset) { }
      });
   }
   @OnClick(R.id.btnBottomSheet)
   public void toggleBottomSheet() {
      if (sheetBehavior.getState() !=
         BottomSheetBehavior.STATE_EXPANDED) {
            sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            btnBottomSheet.setText("Close sheet");
         } else {
         sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
         btnBottomSheet.setText("Expand sheet");
      }
   }
}

步驟 7 - 將以下程式碼新增到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
   xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.sample">
   <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">
         <intent-filter>
            <action
               android:name="android.intent.action.MAIN" />
            <category
               android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟您的專案中的一個活動檔案,然後單擊工具欄中的執行 播放圖示 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

更新於: 2020年7月2日

317 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告