如何在 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 运行应用程序,请打开您的项目中的一个活动文件,然后单击工具栏中的运行 图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -
广告