在Android中,可以使用布局文件来创建视图并进行显示。下面是一种实现方式:

  1. 创建计算器按键视图的布局文件(例如calculator_keypad.xml):
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/btn_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <!-- 添加其它按键按钮 -->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <Button
            android:id="@+id/btn_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <!-- 添加其它按键按钮 -->

    </LinearLayout>

    <!-- 添加其它按键按钮布局 -->

</LinearLayout>
  1. 在主页面布局文件(例如activity_main.xml)中,引入计算器按键视图布局文件:
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 添加其他视图元素 -->

    <LinearLayout
        android:id="@+id/keypadLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <!-- 引入计算器按键视图布局 -->
        <include layout="@layout/calculator_keypad" />

    </LinearLayout>

</RelativeLayout>
  1. 在主页面的Activity中,设置计算器按键视图的按钮点击事件:
public class MainActivity extends AppCompatActivity {

    private Button btn1, btn2, btn3, btn4;
    // 添加其他按钮的声明

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn3 = findViewById(R.id.btn_3);
        btn4 = findViewById(R.id.btn_4);
        // 初始化其他按钮

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理按钮1的点击事件
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理按钮2的点击事件
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理按钮3的点击事件
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理按钮4的点击事件
            }
        });

        // 设置其他按钮的点击事件
    }
}

通过以上步骤,我们可以在主页面下方显示计算器按键视图,并设置按钮的点击事件处理。你可以根据实际需求和设计,修改布局文件和按钮的点击事件处理逻辑

创建一个计算器按键视图然后创建一个主页面在主页面下方显示按键视图在Android中

原文地址: https://www.cveoy.top/t/topic/hE20 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录