评论

收藏

[办公软件] 用Android Studio制作一个简易的计算器,android开发者论坛

电脑办公 电脑办公 发布于:2021-12-25 12:05 | 阅读数:253 | 评论:0

android:columnCount="4"
android:orientation="horizontal"
tools:context=".MainActivity"
tools:ignore="GridLayout">
<!-- orientation="horizontal"水平布局
"vertical"   垂直布局
*rowCount   最大行数
*columnCount 最大列数
*alignmentMode 对齐模式-->
</GridLayout>
接着是计算器的各个按钮
<EditText
android:id="@+id/edit"
android:layout_gravity="right|bottom"
android:text="0"
android:textSize="35dp"
android:layout_columnSpan="4"
android:layout_rowWeight="2"
android:layout_columnWeight="1"
android:background="@null"/>
<TextView
android:id="@+id/view"
android:layout_gravity="right|bottom"
android:text="0"
android:textSize="55dp"
android:layout_columnSpan="4"
android:layout_rowWeight="2"
android:layout_columnWeight="1"/>
<Button
android:id="@+id/add"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="+"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/substact"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="-"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/multiply"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="×"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/divide"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="÷"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/left"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="("
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/seven"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="7"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/eight"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="8"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/nine"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="9"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/right"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text=")"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/four"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="4"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/five"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="5"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/six"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="6"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/equal"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="="
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/three"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="3"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/two"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="2"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/one"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:textSize="24dp"
android:text="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/dot"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="."
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/zero"
android:layout_margin="1dp"
android:background="#FAFAFA"
android:textColor="#7C7D82"
android:text="0"
android:textSize="24dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
<Button
android:id="@+id/Clean"
android:layout_margin="1dp"
android:background="#FB7730"
android:textColor="#FFFFFF"
android:text="C"
android:textSize="24dp"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
/>
然后我们开一下呈现出来的图是什么样子的(如下)
DSC0000.jpg

到这里我们就完成了1/3的工程,紧接着是让各个数字符号对应到各个按钮
[]( )2,连接部分(Main_Activity.java)
参考原文博客连接部分运用的是FindViewByMe(XML)这个插件,连接按钮变得十分方便。建立按钮后,如果每个都要单独设置监听事件会十分麻烦,所以有一个方法onClick方法,多个按钮一起监听。但是在使用此方法是必须连接View.OnClickListener接口。然后set后即可使用。
首先建立按钮(这只是单单的建议按钮,连接按钮的代码在底下)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private GridLayout gridlayout;
private EditText edit;
private TextView view;
private Button add;
private Button substact;
private Button multiply;
private Button divide;
private Button seven;
private Button eight;
private Button nine;
private Button left;
private Button four;
private Button five;
private Button six;
private Button right;
private Button three;
private Button two;
private Button one;
private Button Clean;
private Button dot;
private Button zero;
private Button equal;
}
然后把每个按钮和对应的数字符号对应起来
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridlayout = (GridLayout) findViewById(R.id.gridlayout);
edit = (EditText) findViewById(R.id.edit);
view = (TextView) findViewById(R.id.view);
add = (Button) findViewById(R.id.add);
substact = (Button) findViewById(R.id.substact);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
seven = (Button) findViewById(R.id.seven);
eight = (Button) findViewById(R.id.eight);
nine = (Button) findViewById(R.id.nine);
left = (Button) findViewById(R.id.left);
four = (Button) findViewById(R.id.four);
five = (Button) findViewById(R.id.five);
six = (Button) findViewById(R.id.six);
right = (Button) findViewById(R.id.right);
three = (Button) findViewById(R.id.three);
two = (Button) findViewById(R.id.two);
one = (Button) findViewById(R.id.one);
Clean = (Button) findViewById(R.id.Clean);
dot = (Button) findViewById(R.id.dot);
zero = (Button) findViewById(R.id.zero);
equal = (Button) findViewById(R.id.equal);
edit.setOnClickListener(this);
view.setOnClickListener(this);
add.setOnClickListener(this);
multiply.setOnClickListener(this);
divide.setOnClickListener(this);
dot.setOnClickListener(this);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
zero.setOnClickListener(this);
right.setOnClickListener(this);
Clean.setOnClickListener(this);
left.setOnClickListener(this);
equal.setOnClickListener(this);
substact.setOnClickListener(this);
}
记录上一个结果
private String lastresult;
判断当前是否进行计算
private boolean now;
点击按钮后会输出相对应的数字和符号(onClick)
@Override
public void onClick(View v) {
String text = edit.getText().toString();//获取当前文本编译框内容
String lasttext;
boolean clean = false;
Formulautil a = new Formulautil();
if (text.equals("0"))
{
edit.setText("");
}
switch(v.getId())
{
case R.id.zero:
edit.setText(edit.getText() + "0");
Clean.setText("C");
now = false;
break;
case R.id.one:
edit.setText(edit.getText() + "1");
Clean.setText("C");
now = false;
break;
case R.id.two:
edit.setText(edit.getText() + "2");
Clean.setText("C");
now = false;
break;
case R.id.three:
edit.setText(edit.getText() + "3");
Clean.setText("C");
now = false;
break;
case R.id.four:
edit.setText(edit.getText() + "4");
Clean.setText("C");
now = false;
break;
case R.id.five:
edit.setText(edit.getText() + "5");
Clean.setText("C");
now = false;
break;
case R.id.six:
edit.setText(edit.getText() + "6");
Clean.setText("C");
now = false;
break;
case R.id.seven:
edit.setText(edit.getText() + "7");
Clean.setText("C");
now = false;
break;
case R.id.eight:
edit.setText(edit.getText() + "8");
Clean.setText("C");
now = false;
break;
case R.id.nine:
edit.setText(edit.getText() + "9");
Clean.setText("C");
now = false;
break;
case R.id.dot:
edit.setText(edit.getText() + ".");
Clean.setText("C");
now = false;
break;
case R.id.right:
edit.setText(edit.getText() + ")");
Clean.setText("C");
now = false;
break;
case R.id.left:
edit.setText(edit.getText() + "(");
Clean.setText("C");
now = false;
break;
case R.id.add:
edit.setText(edit.getText() + "+");
Clean.setText("C");
now = false;
break;
case R.id.substact:
edit.setText(edit.getText() + "-");
Clean.setText("C");
now = false;
break;
case R.id.multiply:
edit.setText(edit.getText() + "×");
Clean.setText("C");
now = false;
break;
case R.id.divide:
edit.setText(edit.getText() + "÷");
Clean.setText("C");
now = false;
break;
case R.id.equal:
edit.setText(edit.getText() + "=");
lasttext = edit.getText().toString();
String a1 = String.valueOf(a.caculate(lasttext));
最后
由于题目很多整理答案的工作量太大,所以仅限于提供知识点,详细的很多问题和参考答案我都整理成了 PDF文件,需要的小伙伴可以私信我【面试】免费领取或者点击GitHub免费获取!
DSC0001.jpg

DSC0002.jpg

DSC0003.jpg



关注下面的标签,发现更多相似文章