Android学习:计算器界面

计算器界面图:layout代码如下:calculatordemo.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"...
摘要由CSDN通过智能技术生成

计算器界面图:
在这里插入图片描述
layout代码如下:
calculatordemo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="150dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.91"
        android:minHeight="150dp" >
    </LinearLayout>

    <LinearLayout
        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/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="mc" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="m+" />

            <Button
                android:id="@+id/button5"
                and
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android计算器界面的示例代码: XML布局文件: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="0" android:textSize="36sp" android:padding="16dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="C" /> <Button android:id="@+id/btn_del" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="DEL" /> <Button android:id="@+id/btn_divide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="/" /> <Button android:id="@+id/btn_multiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="*" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/btn_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/btn_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/btn_minus" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/btn_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/btn_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/btn_plus" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <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" /> <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_equal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" /> </LinearLayout> </LinearLayout> ``` Java代码: ```java public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView result; private String currentNumber = ""; private String lastOperator = ""; private boolean hasOperator = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = findViewById(R.id.result); findViewById(R.id.btn_clear).setOnClickListener(this); findViewById(R.id.btn_del).setOnClickListener(this); findViewById(R.id.btn_divide).setOnClickListener(this); findViewById(R.id.btn_multiply).setOnClickListener(this); findViewById(R.id.btn_minus).setOnClickListener(this); findViewById(R.id.btn_plus).setOnClickListener(this); findViewById(R.id.btn_equal).setOnClickListener(this); findViewById(R.id.btn_0).setOnClickListener(this); findViewById(R.id.btn_1).setOnClickListener(this); findViewById(R.id.btn_2).setOnClickListener(this); findViewById(R.id.btn_3).setOnClickListener(this); findViewById(R.id.btn_4).setOnClickListener(this); findViewById(R.id.btn_5).setOnClickListener(this); findViewById(R.id.btn_6).setOnClickListener(this); findViewById(R.id.btn_7).setOnClickListener(this); findViewById(R.id.btn_8).setOnClickListener(this); findViewById(R.id.btn_9).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_clear: currentNumber = ""; lastOperator = ""; hasOperator = false; result.setText("0"); break; case R.id.btn_del: if (currentNumber.length() > 0) { currentNumber = currentNumber.substring(0, currentNumber.length() - 1); result.setText(currentNumber); } break; case R.id.btn_divide: case R.id.btn_multiply: case R.id.btn_minus: case R.id.btn_plus: if (hasOperator) { currentNumber = currentNumber.substring(0, currentNumber.length() - 1) + v.getTag().toString(); } else { currentNumber = currentNumber + v.getTag().toString(); } lastOperator = v.getTag().toString(); hasOperator = true; result.setText(currentNumber); break; case R.id.btn_equal: if (lastOperator == "") { break; } String[] numbers = currentNumber.split("\\" + lastOperator); double resultNumber = 0; switch (lastOperator) { case "/": resultNumber = Double.parseDouble(numbers[0]) / Double.parseDouble(numbers[1]); break; case "*": resultNumber = Double.parseDouble(numbers[0]) * Double.parseDouble(numbers[1]); break; case "-": resultNumber = Double.parseDouble(numbers[0]) - Double.parseDouble(numbers[1]); break; case "+": resultNumber = Double.parseDouble(numbers[0]) + Double.parseDouble(numbers[1]); break; } currentNumber = String.valueOf(resultNumber); lastOperator = ""; hasOperator = false; result.setText(currentNumber); break; default: currentNumber = currentNumber + v.getTag().toString(); result.setText(currentNumber); break; } } } ``` 这个简单的示例演示了如何实现一个基本的计算器界面和计算逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值