赞
踩
在学习了基本布局和点击监听后,可以写一个简单计算器,实现基础的演算功能。
由于计算器的组件基本都是对齐的,因此父布局使用了网格布局。计算结果使用TextView显示,按钮就使用Button即可。
设置行数、列数:android:rowCount="6"/android:columnCount"4"
设置组件横向横跨:android:layout_columnSpan="2" //设置在子组件中
- <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:rowCount="6"
- android:columnCount="4"
- tools:context=".activity.ComputeActivity">
-
- <TextView
- android:id="@+id/compute_textview"
- android:layout_columnSpan="4"
- android:layout_gravity="fill"
- android:background="@drawable/computetextview_style"
- android:text="0"
- android:textSize="40sp"
- android:layout_margin="10dp"
- android:gravity="right"></TextView>
-
- <Button
- android:id="@+id/back"
- android:layout_gravity="fill"
- android:layout_columnSpan="2"
- android:text="回退">
- </Button>
-
- <Button
- android:id="@+id/clear"
- android:layout_gravity="fill"
- android:layout_columnSpan="2"
- android:text="清空">
- </Button>
-
- <Button
- android:id="@+id/plus"
- android:text="+" />
- <Button
- android:id="@+id/num1"
- android:text="1" />
- <Button
- android:id="@+id/num2"
- android:text="2" />
- <Button
- android:id="@+id/num3"
- android:text="3" />
- <Button
- android:id="@+id/less"
- android:text="-" />
- <Button
- android:id="@+id/num4"
- android:text="4" />
- <Button android:id="@+id/num5"
- android:text="5" />
- <Button
- android:id="@+id/num6"
- android:text="6" />
- <Button
- android:id="@+id/multiply"
- android:text="*" />
- <Button
- android:id="@+id/num7"
- android:text="7" />
- <Button
- android:id="@+id/num8"
- android:text="8" />
- <Button
- android:id="@+id/num9"
- android:text="9" />
- <Button
- android:id="@+id/divide"
- android:text="/" />
- <Button
- android:id="@+id/point"
- android:text="." />
- <Button
- android:id="@+id/num0"
- android:text="0" />
- <Button android:id="@+id/equal"
- android:text="=" />
- </GridLayout>
为了好看,这里给TextView设置了自定义shape背景
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <stroke android:color="#E93A3A3A"
- android:width="3sp"/>
- <corners android:radius="10sp"/>
- <padding android:bottom="5px"
- android:top="5dp"
- android:left="5dp"
- android:right="5dp"/>
- </shape>
在Activity的onCreate方法中加载布局文件并为其中所有的Button组件设置点击监听
- private static String TAG = ComputeActivity.class.getSimpleName();
- private TextView textView;
- private float ansNum=0;
- private char op='=';
-
- @SuppressLint("MissingInflatedId")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_compute);
- textView = findViewById(R.id.compute_textview);
- NumberListener numberListener = new NumberListener();
- OtherLister otherLister = new OtherLister();
- findViewById(R.id.num1).setOnClickListener(numberListener);
- findViewById(R.id.num2).setOnClickListener(numberListener);
- findViewById(R.id.num3).setOnClickListener(numberListener);
- findViewById(R.id.num4).setOnClickListener(numberListener);
- findViewById(R.id.num5).setOnClickListener(numberListener);
- findViewById(R.id.num6).setOnClickListener(numberListener);
- findViewById(R.id.num7).setOnClickListener(numberListener);
- findViewById(R.id.num8).setOnClickListener(numberListener);
- findViewById(R.id.num9).setOnClickListener(numberListener);
- findViewById(R.id.num0).setOnClickListener(numberListener);
- findViewById(R.id.point).setOnClickListener(numberListener);
- findViewById(R.id.divide).setOnClickListener(otherLister);
- findViewById(R.id.multiply).setOnClickListener(otherLister);
- findViewById(R.id.plus).setOnClickListener(otherLister);
- findViewById(R.id.less).setOnClickListener(otherLister);
- findViewById(R.id.equal).setOnClickListener(otherLister);
- findViewById(R.id.back).setOnClickListener(otherLister);
- findViewById(R.id.clear).setOnClickListener(otherLister);
- }
创建两个内部类实现View.OnClickListener的onClick方法
- private class NumberListener implements View.OnClickListener{
- @Override
- public void onClick(View view) {
- String ans = (String) textView.getText();
- String n="";
- switch(view.getId()){
- case R.id.num1:
- n = "1";
- break;
- case R.id.num2:
- n = "2";
- break;
- case R.id.num3:
- n = "3";
- break;
- case R.id.num4:
- n = "4";
- break;
- case R.id.num5:
- n = "5";
- break;
- case R.id.num6:
- n = "6";
- break;
- case R.id.num7:
- n = "7";
- break;
- case R.id.num8:
- n = "8";
- break;
- case R.id.num9:
- n = "9";
- break;
- case R.id.num0:
- n = "0";
- break;
- case R.id.point:
- n=".";
- break;
- }
- if(ans.equals("0")&&!n.equals("."))
- textView.setText(n);
- else
- textView.setText(ans.concat(n));
- }
- }
- private class OtherLister implements View.OnClickListener {
- @Override
- public void onClick(View view) {
- String ans = (String) textView.getText();
- //String n = "";
- switch (view.getId()) {
- case R.id.back:
- textView.setText(ans.substring(0, ans.length() - 1));
- break;
- case R.id.clear:
- textView.setText("0");
- ansNum=0;
- op='=';
- break;
- case R.id.divide:
- computeNumber(Float.parseFloat((String) textView.getText()));
- textView.setText("0");
- op = '/';
- break;
- case R.id.multiply:
- computeNumber(Float.parseFloat((String) textView.getText()));
- textView.setText("0");
- op = '*';
- break;
- case R.id.plus:
- computeNumber(Float.parseFloat((String) textView.getText()));
- textView.setText("0");
- op = '+';
- break;
- case R.id.less:
- computeNumber(Float.parseFloat((String) textView.getText()));
- textView.setText("0");
- op = '-';
- break;
- case R.id.equal:
- computeNumber(Float.parseFloat((String) textView.getText()));
- textView.setText(String.valueOf(ansNum));
- op='=';
- break;
- }
- }
- }
-
-
-
- private void computeNumber(float num) {
- switch (op) {
- case '+':
- ansNum+=num;
- break;
- case '-':
- ansNum-=num;
- break;
- case '*':
- ansNum*=num;
- break;
- case '/':
- ansNum/=num;
- break;
- default:
- ansNum=num;
- }
- Log.d("compute",op+"");
- Log.d("compute",String.valueOf(ansNum));
- }
现在,这个简单计算器已经可以正常运行了,与真正的计算器相比还是十分简陋的,主要是学习布局和监听事件。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。