Android Studio计算器

---恢复内容开始---

从计算器的例子中学到,
1 导入相应包声明alt+enter
2 ctrl+o对父类方法进行重写
3 删除一行ctrl+x
4 padding表示内边距

5 ctrl+alt+o去掉导入的多余的包
布局文件写在layout布局中,layout中的布局背景颜色在drawable中,drawable中引用的颜色在values中的color.xml中

取出显示的内容String str=et_input.getText().toString()

 

MainActivity

  1 package calcluatedemo.example.administrator.calcluatedemo;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.view.View;
  6 import android.widget.Button;
  7 import android.widget.EditText;
  8 
  9 public class MainActivity extends Activity implements View.OnClickListener {/*导入相应声明alt+enter*/
 10     Button btn_0;
 11     Button btn_1;
 12     Button btn_2;
 13     Button btn_3;
 14     Button btn_4;
 15     Button btn_5;
 16     Button btn_6;
 17     Button btn_7;
 18     Button btn_8;
 19     Button btn_9;
 20     Button btn_reduce;
 21     Button btn_equal;
 22     Button btn_add;
 23     Button btn_point;
 24     Button btn_clear;
 25     Button btn_del;
 26     Button btn_divide;
 27     Button btn_mul;
 28     EditText et_input;
 29     boolean clear_flag;
 30     @Override
 31     protected void onCreate(Bundle savedInstanceState) {
 32         super.onCreate(savedInstanceState);
 33         setContentView(R.layout.activity_main);
 34         btn_0=(Button)findViewById(R.id.btn_0);
 35         btn_1=(Button)findViewById(R.id.btn_1);
 36         btn_2=(Button)findViewById(R.id.btn_2);
 37         btn_3=(Button)findViewById(R.id.btn_3);
 38         btn_4=(Button)findViewById(R.id.btn_4);
 39         btn_5=(Button)findViewById(R.id.btn_5);
 40         btn_6=(Button)findViewById(R.id.btn_6);
 41         btn_7=(Button)findViewById(R.id.btn_7);
 42         btn_8=(Button)findViewById(R.id.btn_8);
 43         btn_9=(Button)findViewById(R.id.btn_9);
 44         btn_reduce=(Button)findViewById(R.id.btn_reduce);
 45         btn_equal=(Button)findViewById(R.id.btn_equal);
 46         btn_add=(Button)findViewById(R.id.btn_add);
 47         btn_point=(Button)findViewById(R.id.btn_point);
 48         btn_clear=(Button)findViewById(R.id.btn_clear);
 49         btn_del=(Button)findViewById(R.id.btn_del);
 50         btn_divide=(Button)findViewById(R.id.btn_divide);
 51         btn_mul=(Button)findViewById(R.id.btn_mul);
 52         et_input= (EditText) findViewById(R.id.et_input);
 53         btn_0.setOnClickListener(this);
 54         btn_1.setOnClickListener(this);
 55         btn_2.setOnClickListener(this);
 56         btn_3.setOnClickListener(this);
 57         btn_4.setOnClickListener(this);
 58         btn_5.setOnClickListener(this);
 59         btn_6.setOnClickListener(this);
 60         btn_7.setOnClickListener(this);
 61         btn_8.setOnClickListener(this);
 62         btn_9.setOnClickListener(this);
 63         btn_del.setOnClickListener(this);
 64         btn_divide.setOnClickListener(this);
 65         btn_reduce.setOnClickListener(this);
 66         btn_equal.setOnClickListener(this);
 67         btn_mul.setOnClickListener(this);
 68         btn_point.setOnClickListener(this);
 69         btn_clear.setOnClickListener(this);
 70     }
 71 /*ctrl+o对父类方法进行重写*/
 72     @Override
 73     public void onClick(View v) {
 74         String str=et_input.getText().toString();/*取出显示的内容*/
 75         switch (v.getId()){
 76             case R.id.btn_0:
 77             case R.id.btn_1:
 78             case R.id.btn_2:
 79             case R.id.btn_3:
 80             case R.id.btn_4:
 81             case R.id.btn_5:
 82             case R.id.btn_6:
 83             case R.id.btn_7:
 84             case R.id.btn_8:
 85             case R.id.btn_9:
 86             case R.id.btn_point:
 87             if(clear_flag){
 88                 clear_flag=false;
 89                 str="";
 90                 et_input.setText("");
 91             }
 92                 et_input.setText(str+((Button)v).getText());
 93                 break;
 94             case R.id.btn_reduce:
 95             case R.id.btn_add:
 96             case R.id. btn_divide:
 97             case R.id.btn_mul:
 98                 if(clear_flag){
 99                     clear_flag=false;
100                     str="";
101                     et_input.setText("");
102                 }
103                 et_input.setText(str+" "+((Button)v).getText()+" ");
104                 break;
105             case R.id.btn_clear:
106 
107                 et_input.setText("");
108                 break;
109             case R.id.btn_del:
110                 if(clear_flag){
111                     clear_flag=false;
112                     str="";
113                     et_input.setText("");
114                 }
115                 if(str!=null&&!str.equals("")) {
116                     et_input.setText(str.substring(0,str.length() - 1));
117                 }
118                 break;
119             case R.id.btn_equal:
120             getResult();
121                 break;
122         }
123 
124     }
125     private void getResult(){
126 
127         String exp=et_input.getText().toString();
128         if(exp.equals(" ")&&exp==null){
129             return;
130         }
131         if(!exp.contains(" ")){
132             return;
133         }
134         if(clear_flag){
135             clear_flag=false;
136             return;
137         }
138         clear_flag=true;
139         String str1=exp.substring(0,exp.indexOf(" "));/*第一个数字*/
140         String str3=exp.substring(exp.indexOf(" ")+3);/*第二个数字*/
141         String str2=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);/*运算符*/
142         double result = 0;
143         if(!str1.equals("")&&!str3.equals("")){
144             double s1=Double.parseDouble(str1);
145             double s3=Double.parseDouble(str3);
146         if(str2.equals("+")){
147             result=s1+s3;
148         }else  if(str2.equals("-")){
149             result=s1-s3;
150         }else  if(str2.equals("×")){
151             result=s1*s3;
152         }else  if(str2.equals("÷")){
153             if(s3==0){
154                result=0;
155             }else{
156 
157                 result=s1/s3;
158             }
159 
160         }
161        if(!str1.contains(".")&&!str3.contains(".")&&!str2.equals("÷")){
162             int r=(int)result;
163             et_input.setText(r+"");
164         }else{
165             et_input.setText(result+"");
166         }
167         }
168         else if(str1.equals("")&&!str3.equals("")){
169 
170             double s3=Double.parseDouble(str3);
171         if(str2.equals("+")){
172             result=0+s3;
173         }else  if(str2.equals("-")){
174             result=0-s3;
175         }else  if(str2.equals("×")){
176             result=0;
177         }else  if(str2.equals("÷")){
178 
179                result=0;
180 
181 
182         }
183         if(!str3.contains(".")){
184             int r=(int)result;
185             et_input.setText(r+"");
186         }else{
187             et_input.setText(result+"");
188         }
189         }
190         else if(!str1.equals("")&&str3.equals("")){
191             et_input.setText(exp);
192         }else {
193             et_input.setText("");
194         }
195     }
196 }
197 /*
198 删除一行ctrl+x*/

 layout

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:tools="http://schemas.android.com/tools"
  4     android:layout_width="match_parent"
  5     android:layout_height="match_parent"
  6     android:paddingBottom="@dimen/activity_vertical_margin"
  7     android:paddingLeft="@dimen/activity_horizontal_margin"
  8     android:paddingRight="@dimen/activity_horizontal_margin"
  9     android:paddingTop="@dimen/activity_vertical_margin"
 10     android:orientation="vertical"
 11     tools:context="calcluatedemo.example.administrator.calcluatedemo.MainActivity">
 12 
 13 
 14     <EditText
 15         android:layout_width="match_parent"
 16         android:layout_height="60dip"
 17         android:background="@drawable/white_bg"
 18         android:id="@+id/et_input"
 19         android:enabled="false"
 20         android:gravity="bottom|right"/><!--gravity,文字在控件中的对齐方式
 21         layout_gravity,控件在布局中对齐方式-->
 22     <LinearLayout
 23         android:layout_width="match_parent"
 24         android:layout_height="wrap_content"
 25         android:layout_marginTop="20dp"
 26         android:orientation="horizontal"
 27         android:gravity="center_horizontal">
 28         <Button
 29             android:layout_width="60dp"
 30             android:layout_height="60dp"
 31             android:text="C"
 32             android:textSize="20sp"
 33             android:gravity="bottom|right"
 34             android:id="@+id/btn_clear"
 35             android:paddingBottom="10dp"
 36             android:paddingRight="10dp"
 37             android:background="@drawable/white_selector"/>
 38         <Button
 39             android:layout_width="60dp"
 40             android:layout_height="60dp"
 41             android:layout_marginLeft="10dp"
 42             android:text="DEL"
 43             android:textSize="20sp"
 44             android:id="@+id/btn_del"
 45             android:gravity="bottom|right"
 46             android:paddingBottom="10dp"
 47             android:paddingRight="10dp"
 48             android:background="@drawable/white_selector"/>
 49         <Button
 50             android:layout_width="60dp"
 51             android:layout_height="60dp"
 52             android:layout_marginLeft="10dp"
 53             android:text="÷"
 54             android:textSize="20sp"
 55             android:gravity="bottom|right"
 56             android:paddingBottom="10dp"
 57             android:paddingRight="10dp"
 58             android:id="@+id/btn_divide"
 59             android:background="@drawable/white_selector"/>
 60         <Button
 61             android:layout_width="60dp"
 62             android:layout_height="60dp"
 63             android:layout_marginLeft="10dp"
 64             android:paddingBottom="10dp"
 65             android:paddingRight="10dp"
 66             android:text="×"
 67             android:textSize="20sp"
 68             android:gravity="bottom|right"
 69             android:id="@+id/btn_mul"
 70             android:background="@drawable/white_selector"/>
 71 
 72     </LinearLayout>
 73     <LinearLayout
 74         android:layout_width="match_parent"
 75         android:layout_height="wrap_content"
 76         android:layout_marginTop="10dp"
 77         android:orientation="horizontal"
 78         android:gravity="center_horizontal">
 79         <Button
 80             android:layout_width="60dp"
 81             android:layout_height="60dp"
 82             android:text="7"
 83             android:textSize="20sp"
 84             android:gravity="bottom|right"
 85             android:id="@+id/btn_7"
 86             android:paddingBottom="10dp"
 87             android:paddingRight="10dp"
 88             android:background="@drawable/white_selector"/>
 89         <Button
 90             android:layout_width="60dp"
 91             android:layout_height="60dp"
 92             android:layout_marginLeft="10dp"
 93             android:text="8"
 94             android:textSize="20sp"
 95             android:id="@+id/btn_8"
 96             android:paddingBottom="10dp"
 97             android:paddingRight="10dp"
 98             android:gravity="bottom|right"
 99             android:background="@drawable/white_selector"/>
100         <Button
101             android:layout_width="60dp"
102             android:layout_height="60dp"
103             android:layout_marginLeft="10dp"
104             android:text="9"
105             android:textSize="20sp"
106             android:gravity="bottom|right"
107             android:id="@+id/btn_9"
108             android:paddingBottom="10dp"
109             android:paddingRight="10dp"
110             android:background="@drawable/white_selector"/>
111         <Button
112             android:layout_width="60dp"
113             android:layout_height="60dp"
114             android:layout_marginLeft="10dp"
115             android:text="-"
116             android:textSize="20sp"
117             android:paddingBottom="10dp"
118             android:paddingRight="10dp"
119             android:gravity="bottom|right"
120             android:id="@+id/btn_reduce"
121             android:background="@drawable/white_selector"/>
122 
123     </LinearLayout>
124     <LinearLayout
125         android:layout_width="match_parent"
126         android:layout_height="wrap_content"
127         android:layout_marginTop="10dp"
128         android:orientation="horizontal"
129         android:gravity="center_horizontal">
130         <Button
131             android:layout_width="60dp"
132             android:layout_height="60dp"
133             android:text="4"
134             android:textSize="20sp"
135             android:gravity="bottom|right"
136             android:id="@+id/btn_4"
137             android:paddingBottom="10dp"
138             android:paddingRight="10dp"
139             android:background="@drawable/white_selector"/>
140         <Button
141             android:layout_width="60dp"
142             android:layout_height="60dp"
143             android:layout_marginLeft="10dp"
144             android:text="5"
145             android:textSize="20sp"
146             android:id="@+id/btn_5"
147             android:gravity="bottom|right"
148             android:paddingBottom="10dp"
149             android:paddingRight="10dp"
150             android:background="@drawable/white_selector"/>
151         <Button
152             android:layout_width="60dp"
153             android:layout_height="60dp"
154             android:layout_marginLeft="10dp"
155             android:text="6"
156             android:textSize="20sp"
157             android:paddingBottom="10dp"
158             android:paddingRight="10dp"
159             android:gravity="bottom|right"
160             android:id="@+id/btn_6"
161             android:background="@drawable/white_selector"/>
162         <Button
163             android:layout_width="60dp"
164             android:layout_height="60dp"
165             android:layout_marginLeft="10dp"
166             android:text="+"
167             android:textSize="20sp"
168             android:paddingBottom="10dp"
169             android:paddingRight="10dp"
170             android:gravity="bottom|right"
171             android:id="@+id/btn_add"
172             android:background="@drawable/white_selector"/>
173 
174 
175     </LinearLayout>
176     <LinearLayout
177         android:layout_width="match_parent"
178         android:layout_height="wrap_content"
179         android:gravity="center"
180         android:orientation="horizontal">
181         <LinearLayout
182             android:layout_width="wrap_content"
183             android:layout_height="wrap_content"
184 
185             android:orientation="vertical"
186             android:gravity="center_horizontal">
187             <LinearLayout
188             android:layout_width="wrap_content"
189             android:layout_height="wrap_content"
190             android:layout_marginTop="10dp"
191             android:orientation="horizontal"
192             android:gravity="center_horizontal">
193             <Button
194                 android:layout_width="60dp"
195                 android:layout_height="60dp"
196                 android:text="1"
197                 android:textSize="20sp"
198                 android:gravity="bottom|right"
199                 android:id="@+id/btn_1"
200                 android:paddingBottom="10dp"
201                 android:paddingRight="10dp"
202                 android:background="@drawable/white_selector"/>
203             <Button
204                 android:layout_width="60dp"
205                 android:layout_height="60dp"
206                 android:layout_marginLeft="10dp"
207                 android:text="2"
208                 android:paddingBottom="10dp"
209                 android:paddingRight="10dp"
210                 android:textSize="20sp"
211                 android:id="@+id/btn_2"
212                 android:gravity="bottom|right"
213                 android:background="@drawable/white_selector"/>
214             <Button
215                 android:layout_width="60dp"
216                 android:layout_height="60dp"
217                 android:layout_marginLeft="10dp"
218                 android:text="3"
219                 android:paddingBottom="10dp"
220                 android:paddingRight="10dp"
221                 android:textSize="20sp"
222                 android:gravity="bottom|right"
223                 android:id="@+id/btn_3"
224                 android:background="@drawable/white_selector"/>
225 
226 
227             </LinearLayout>
228             <LinearLayout
229             android:layout_width="wrap_content"
230             android:layout_height="wrap_content"
231             android:layout_marginTop="10dp"
232             android:orientation="horizontal"
233             android:gravity="center_horizontal">
234             <Button
235                 android:layout_width="130dp"
236                 android:layout_height="60dp"
237                 android:text="0"
238                 android:textSize="20sp"
239                 android:paddingBottom="10dp"
240                 android:paddingRight="10dp"
241                 android:gravity="bottom|right"
242                 android:id="@+id/btn_0"
243                 android:background="@drawable/white_selector"/>
244             <Button
245                 android:layout_width="60dp"
246                 android:layout_height="60dp"
247                 android:layout_marginLeft="10dp"
248                 android:text="."
249                 android:paddingBottom="10dp"
250                 android:paddingRight="10dp"
251                 android:textSize="20sp"
252                 android:id="@+id/btn_point"
253                 android:gravity="bottom|right"
254                 android:background="@drawable/white_selector"/>
255         </LinearLayout>
256         </LinearLayout>
257         <Button
258             android:layout_width="60dp"
259             android:layout_height="130dp"
260             android:layout_marginLeft="10dp"
261             android:layout_marginTop="10dp"
262             android:text="="
263             android:textSize="30sp"
264             android:gravity="bottom|right"
265             android:id="@+id/btn_equal"
266             android:paddingBottom="10dp"
267             android:paddingRight="10dp"
268             android:background="@drawable/orange_selector"/>
269     </LinearLayout>
270 </LinearLayout>
271 <!--padding内边距-->
drawable中代码,主要按压下去颜色,本来颜色,通过selector标签
1
<?xml version="1.0" encoding="utf-8"?> 2 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 3 <item android:drawable="@drawable/ashen" android:state_pressed="true"/><!--按压下去颜色--> 4 <item android:drawable="@drawable/orange"/><!--本来颜色--> 5 </selector>

 

---恢复内容结束---

转载于:https://www.cnblogs.com/633sylss/p/5456913.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值