赞
踩
1.简单的计算器
2.科学计算器
3.程序计算器
不用实现具体功能,只需设计界面即可!
线性布局中添加4个按钮
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <LinearLayout android:layout_width="431dp" android:layout_height="669dp" android:orientation="vertical" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="-4dp" tools:layout_editor_absoluteY="57dp"> <Button android:id="@+id/button21" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="简单计算器" /> <Button android:id="@+id/button22" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="科学计算器" /> <Button android:id="@+id/button23" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="程序计算器" /> <Button android:id="@+id/button24" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="退出" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="7"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center|right" android:text="0" android:textSize="50sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" android:weightSum="4"> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="退格" android:textSize="30sp" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="清除" android:textSize="30sp" /> <Button android:id="@+id/button3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="±" android:textSize="40sp" /> <Button android:id="@+id/button4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="+" android:textSize="40sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/button5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="7" android:textSize="40sp" /> <Button android:id="@+id/button6" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="8" android:textSize="40sp" /> <Button android:id="@+id/button7" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="9" android:textSize="40sp" /> <Button android:id="@+id/button8" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="—" android:textSize="40sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/button9" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="4" android:textSize="40sp"/> <Button android:id="@+id/button10" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="5" android:textSize="40sp"/> <Button android:id="@+id/button11" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="6" android:textSize="40sp"/> <Button android:id="@+id/button12" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="*" android:textSize="40sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/button13" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="1" android:textSize="40sp"/> <Button android:id="@+id/button14" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="2" android:textSize="40sp"/> <Button android:id="@+id/button15" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="3" android:textSize="40sp"/> <Button android:id="@+id/button16" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="/" android:textSize="40sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/button17" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="0" android:textSize="40sp"/> <Button android:id="@+id/button18" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="." android:textSize="40sp"/> <Button android:id="@+id/button19" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="1/x" android:textSize="30sp"/> <Button android:id="@+id/button20" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="=" android:textSize="40sp"/> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|right" android:text="0" android:textSize="50sp"/> <TableLayout android:layout_width="match_parent" android:layout_height="21dp" android:layout_margin="10dp" android:padding="0dp"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="角度" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="弧度" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="梯度" android:textSize="15dp" /> </RadioGroup> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="215dp"> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="SIN" /> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="COS" /> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="TAN" /> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="COT"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ASIN"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ACOS"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ATAN"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ACOT"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="SINH"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="COSH"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="TANH"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="COTH"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ASINH"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ACOSH"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ATANH"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="ACOTH"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="LN"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="LOG10"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="N!"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="X^Y"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="E^X"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="π"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="("></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text=")"></Button> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="186dp"> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="退格"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="清除"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="±"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="+"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="7"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="8"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="9"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="-"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="4"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="5"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="6"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="*"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="1"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="2"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="3"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="/"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="0"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="."></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="1/X"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="="></Button> </TableRow> </TableLayout> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|right" android:text="0" android:textSize="50sp" /> <TableLayout android:layout_width="match_parent" android:layout_height="21dp" android:layout_margin="10dp" android:padding="0dp"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <RadioButton android:layout_width="76dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="十六进制" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="二进制" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="八进制" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="十进制" android:textSize="15dp" /> </RadioGroup> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="21dp" android:layout_margin="10dp" android:padding="0dp"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="八字节" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="四字节" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="二字节" android:textSize="15dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="单字节" android:textSize="15dp" /> </RadioGroup> </TableRow> </TableLayout> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="4" android:orientation="horizontal" android:rowCount="3"> <Button android:layout_width="102dp" android:layout_height="match_parent" android:text="NOT" android:textColor="#FBF9F9" /> <Button android:layout_width="103dp" android:layout_height="match_parent" android:text="AND" /> <Button android:layout_width="104dp" android:layout_height="match_parent" android:text="OR" /> <Button android:layout_width="99dp" android:layout_height="match_parent" android:text="XOR" /> <Button android:layout_width="104dp" android:layout_height="match_parent" android:text="循环左移" /> <Button android:layout_width="104dp" android:layout_height="match_parent" android:text="循环右移" /> <Button android:layout_width="104dp" android:layout_height="match_parent" android:text="左移" /> <Button android:layout_width="102dp" android:layout_height="match_parent" android:text="MOD" /> <Button android:layout_columnSpan="2" android:layout_gravity="fill" android:text="无符号右移" /> <Button android:layout_width="wrap_content" android:layout_columnSpan="2" android:layout_gravity="fill" android:text="有符号右移" /> </GridLayout> <TableLayout android:layout_width="match_parent" android:layout_height="212dp"> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="退格"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="清除"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="9"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="±"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="6"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="7"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="8"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="+"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="3"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="4"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="5"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="+"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="0"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="1"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="2"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="/"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="A"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="B"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="C"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="D"></Button> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="487dp" android:layout_weight="1" android:padding="0dp"> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="E"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="F"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="G"></Button> <Button android:layout_height="match_parent" android:layout_weight="1" android:text="="></Button> </TableRow> </TableLayout> </LinearLayout> </LinearLayout>
点击对应的按钮,跳转相应的界面!
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=(Button) findViewById(R.id.button21); Button btn2=(Button) findViewById(R.id.button22); Button btn3=(Button) findViewById(R.id.button23); Button btn4=(Button) findViewById(R.id.button24); btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View View){ setContentView(R.layout.jiandanjishuanqi); } }); btn2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View View){ setContentView(R.layout.kexuejishuanqi); } }); btn3.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View View){ setContentView(R.layout.chengxujishuanqi); } }); btn4.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View View){ System.exit(0); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event){ if(keyCode==KeyEvent.KEYCODE_BACK){ Intent intent=new Intent(this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } return true; } }
安卓实验作业,刚接触能力有限就做了个大概,可以根据自己的需求添加主题,样式,颜色!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。