赞
踩
android:background=“@drawable/white_selector”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
<Button
android:id=“@+id/bt_add”
android:layout_width=“80dp”
android:layout_height=“80dp”
android:text=“+”
android:textSize=“30sp”
android:gravity=“right|bottom”
android:layout_marginLeft=“10dp”
android:background=“@drawable/white_selector”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”
android:layout_marginTop=“10dp”
android:gravity=“center_horizontal”>
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:orientation=“vertical”
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:orientation=“horizontal”
<Button
android:layout_width=“80dp”
android:layout_height=“80dp”
android:id=“@+id/bt_1”
android:text=“1”
android:textSize=“30sp”
android:gravity=“right|bottom”
android:background=“@drawable/white_selector”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
<Button
android:layout_width=“80dp”
android:layout_height=“80dp”
android:id=“@+id/bt_2”
android:text=“2”
android:textSize=“30sp”
android:gravity=“right|bottom”
android:layout_marginLeft=“10dp”
android:background=“@drawable/white_selector”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
<Button
android:layout_width=“80dp”
android:layout_height=“80dp”
android:id=“@+id/bt_3”
android:text=“3”
android:textSize=“30sp”
android:gravity=“right|bottom”
android:layout_marginLeft=“10dp”
android:background=“@drawable/white_selector”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:orientation=“horizontal”
android:layout_marginTop=“10dp”>
<Button
android:layout_width=“170dp”
android:layout_height=“80dp”
android:id=“@+id/bt_0”
android:text=“0”
android:textSize=“30sp”
android:gravity=“right|bottom”
android:background=“@drawable/white_selector”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
<Button
android:layout_width=“80dp”
android:layout_height=“80dp”
android:id=“@+id/bt_pt”
android:text=“.”
android:textSize=“30sp”
android:gravity=“right|bottom”
android:layout_marginLeft=“10dp”
android:background=“@drawable/white_selector”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
<Button
android:id=“@+id/bt_eq”
android:layout_width=“80dp”
android:layout_height=“170dp”
android:layout_marginLeft=“10dp”
android:background=“@drawable/orange_selector”
android:gravity=“right|bottom”
android:text=“=”
android:textSize=“30sp”
android:paddingRight=“15sp”
android:paddingBottom=“15sp”
/>
Mainactivity的代码:
package com.example.administrator.calculatordemo;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener{
Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_pt;
Button bt_mul,bt_div,bt_add,bt_sub;
Button bt_clr,bt_del,bt_eq;
EditText et_input;
boolean clr_flag; //判断et中是否清空
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化对象
setContentView(R.layout.activity_main);
bt_0= (Button) findViewById(R.id.bt_0);
bt_1= (Button) findViewById(R.id.bt_1);
bt_2= (Button) findViewById(R.id.bt_2);
bt_3= (Button) findViewById(R.id.bt_3);
bt_4= (Button) findViewById(R.id.bt_4);
bt_5= (Button) findViewById(R.id.bt_5);
bt_6= (Button) findViewById(R.id.bt_6);
bt_7= (Button) findViewById(R.id.bt_7);
bt_8= (Button) findViewById(R.id.bt_8);
bt_9= (Button) findViewById(R.id.bt_9);
bt_pt= (Button) findViewById(R.id.bt_pt);
bt_add= (Button) findViewById(R.id.bt_add);
bt_sub= (Button) findViewById(R.id.bt_sub);
bt_mul= (Button) findViewById(R.id.bt_mul);
bt_div= (Button) findViewById(R.id.bt_div);
bt_clr= (Button) findViewById(R.id.bt_clr);
bt_del= (Button) findViewById(R.id.bt_del);
bt_eq= (Button) findViewById(R.id.bt_eq);
et_input= (EditText) findViewById(R.id.et_input);
//设置按钮的点击事件
bt_0.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
bt_pt.setOnClickListener(this);
bt_add.setOnClickListener(this);
bt_sub.setOnClickListener(this);
bt_mul.setOnClickListener(this);
bt_div.setOnClickListener(this);
bt_clr.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_eq.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str=et_input.getText().toString();
switch (v.getId()){
case R.id.bt_0:
case R.id.bt_1:
case R.id.bt_2:
case R.id.bt_3:
case R.id.bt_4:
case R.id.bt_5:
case R.id.bt_6:
case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9:
case R.id.bt_pt:
if(clr_flag){
clr_flag=false;
str=“”;
et_input.setText(“”);
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.bt_add:
case R.id.bt_sub:
case R.id.bt_mul:
case R.id.bt_div:
if(clr_flag){
clr_flag=false;
str=“”;
et_input.setText(“”);
}
if(str.contains(“+”)||str.contains(“-”)||str.contains(“×”)||str.contains(“÷”)) {
str=str.substring(0,str.indexOf(" "));
}
et_input.setText(str+" “+((Button)v).getText()+” ");
break;
case R.id.bt_clr:
if(clr_flag)
clr_flag=false;
str=“”;
et_input.setText(“”);
break;
case R.id.bt_del: //判断是否为空,然后在进行删除
if(clr_flag){
clr_flag=false;
str=“”;
et_input.setText(“”);
}
else if(str!=null&&!str.equals(“”)){
et_input.setText(str.substring(0,str.length()-1));
}
break;
case R.id.bt_eq: //单独运算最后结果
getResult();
break;
}
}
private void getResult(){
String exp=et_input.getText().toString();
if(exp==null||exp.equals(“”)) return ;
//因为没有运算符所以不用运算
if(!exp.contains(" ")){
return ;
}
if(clr_flag){
clr_flag=false;
return;
}
clr_flag=true;
//截取运算符前面的字符串
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从哪里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家
我的【Github】会分享一些关于Android进阶方面的知识,也会分享一下最新的面试题~
如果你熟练掌握GitHub中列出的知识点,相信将会大大增加你通过前两轮技术面试的几率!这些内容都供大家参考,互相学习。
①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以在我的【Github】阅读下载,最后觉得有帮助、有需要的朋友可以点个赞
外链图片转存中…(img-EPO3xZIy-1710498915137)]
很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从哪里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家
我的【Github】会分享一些关于Android进阶方面的知识,也会分享一下最新的面试题~
如果你熟练掌握GitHub中列出的知识点,相信将会大大增加你通过前两轮技术面试的几率!这些内容都供大家参考,互相学习。
①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以在我的【Github】阅读下载,最后觉得有帮助、有需要的朋友可以点个赞
[外链图片转存中…(img-fq5yCvor-1710498915137)]
[外链图片转存中…(img-Qy5aozlE-1710498915138)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。