Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android 小項目之--CheckBox<又>計算器 (附源碼)

Android 小項目之--CheckBox<又>計算器 (附源碼)

編輯:Android開發實例

今天的計算器用到的事件比前者多了幾個,而且也結合了多控件事件操作的詳細代碼例子,園友們可以通過一個例子學會使用多個控件,這也是今天發布這篇博文的用意所在。今天用到的不是Button而是改用了CheckBox使用的是例用CheckBox的OnCheckedChangeListener 做事件觸發,最終效果如圖:

下麵我將一一講解代碼思路:首先從兩個文本框開始,先上代碼

 

 1  list=new OnKeyListener() {
 2             
 3             @Override
 4             public boolean onKey(View v, int keyCode, KeyEvent event) {
 5                 // TODO Auto-generated method stub 
 6                     if(mBox1.isChecked())
 7                     {
 8                          mBox1.setChecked(false);
 9                     } 
10                     if(mBox2.isChecked())
11                     {
12                          mBox2.setChecked(false);
13                     } 
14                     if(mBox3.isChecked())
15                     {
16                          mBox3.setChecked(false);
17                     } 
18                     if(mBox4.isChecked())
19                     {
20                          mBox4.setChecked(false);
21                     }
22                      
23                 return false;
24             }
25         };
26    mEditText.setOnKeyListener(list);
27    mEditText1.setOnKeyListener(list);

 

上述代碼用意在於,當用戶之前選過計算方法,想從新輸入時,將會激發文本的onkey事件,在事件激發後我們要做的就是如果某一checkbox選中我們將讓他失去選中的效果,以便當用戶輸入完成後可以重新點擊計算結果。

功能代碼二:

 1  listner=new OnCheckedChangeListener() {
 2             
 3             @Override
 4             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 5                 // TODO Auto-generated method stub
 6                 switch (buttonView.getId()) {
 7                 case R.id.Plus:
 8                     if(!isEmpty(mEditText,mEditText1))
 9                     {
10                         Confirm();
11                          mBox1.setChecked(false);
12                         return;
13                     }
14                     break;
15                 case R.id.Cut:
16                     if(!isEmpty(mEditText,mEditText1))
17                     {
18                         Confirm();
19                          mBox2.setChecked(false);
20                         return;
21                     }
22                     break;
23                 case R.id.Ride:
24                     if(!isEmpty(mEditText,mEditText1))
25                     {
26                         Confirm();
27                          mBox3.setChecked(false);
28                         return;
29                     }
30                     break;
31                 case R.id.Except:
32                     if(!isEmpty(mEditText,mEditText1))
33                     {
34                         Confirm();
35                          mBox4.setChecked(false);
36                         return;
37                     }
38                     break;
39                 default:
40                     break;
41                 }
42                 
43                 if(mBox1.isChecked())
44                 {
45                     mTextView.setText( GetOperation(" "));
46                 }
47                 else
48                 {
49                     mTextView.setText("");
50                 }
51                 if(mBox2.isChecked())
52                 {
53                     mTextView2.setText(GetOperation("-"));
54                 }
55                 else
56                 {
57                     mTextView2.setText("");
58                 }
59                 if(mBox3.isChecked())
60                 {
61                     mTextView3.setText(GetOperation("*"));
62                 }
63                 else
64                 {
65                     mTextView3.setText("");
66                 }
67                 if(mBox4.isChecked())
68                 {
69                     mTextView4.setText(GetOperation("/"));
70                 }
71                 else
72                 {
73                     mTextView4.setText("");
74                 }
75             }
76         };
77         mBox1.setOnCheckedChangeListener(listner);
78         mBox2.setOnCheckedChangeListener(listner);
79         mBox3.setOnCheckedChangeListener(listner);
80         mBox4.setOnCheckedChangeListener(listner);

 

 

如果用戶選中將根據選中的某一個checkbox返回計算結果,如果uncheck將清空文本.做這一步之前必須限制兩個文本框必須輸入,否則程序會崩潰。

全部代碼如下:

  1 package cn.terry;
  2 
  3 import android.app.Activity;
  4 import android.app.AlertDialog;
  5 import android.content.DialogInterface; 
  6 import android.os.Bundle;  
  7 import android.view.KeyEvent;
  8 import android.view.View;
  9 import android.view.View.OnKeyListener;
 10 import android.widget.*;
 11 import android.widget.CompoundButton.OnCheckedChangeListener;
 12 public class CheckBoxCalc extends Activity {
 13     private TextView mTextView;
 14     private TextView mTextView2;
 15     private TextView mTextView3;
 16     private TextView mTextView4;
 17     private CheckBox mBox1;
 18     private CheckBox mBox2;
 19     private CheckBox mBox3;
 20     private CheckBox mBox4;
 21     private EditText mEditText;
 22     private EditText mEditText1;
 23     private boolean isbool=true;
 24     private OnCheckedChangeListener listner; 
 25     private Float Temp;
 26     private String Experssion;
 27     private OnKeyListener list;
 28     /** Called when the activity is first created. */
 29     @Override
 30     public void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.main);
 33         mTextView=(TextView)findViewById(R.id.result1);
 34         mTextView2=(TextView)findViewById(R.id.result2);
 35         mTextView3=(TextView)findViewById(R.id.result3);
 36         mTextView4=(TextView)findViewById(R.id.result4);
 37         mBox1=(CheckBox)findViewById(R.id.Plus);  //加減乘除
 38         mBox2=(CheckBox)findViewById(R.id.Cut);
 39         mBox3=(CheckBox)findViewById(R.id.Ride);
 40         mBox4=(CheckBox)findViewById(R.id.Except);
 41         mEditText=(EditText)findViewById(R.id.first);
 42         mEditText1=(EditText)findViewById(R.id.second);
 43         list=new OnKeyListener() {
 44             
 45             @Override
 46             public boolean onKey(View v, int keyCode, KeyEvent event) {
 47                 // TODO Auto-generated method stub 
 48                     if(mBox1.isChecked())
 49                     {
 50                          mBox1.setChecked(false);
 51                     } 
 52                     if(mBox2.isChecked())
 53                     {
 54                          mBox2.setChecked(false);
 55                     } 
 56                     if(mBox3.isChecked())
 57                     {
 58                          mBox3.setChecked(false);
 59                     } 
 60                     if(mBox4.isChecked())
 61                     {
 62                          mBox4.setChecked(false);
 63                     }
 64                      
 65                 return false;
 66             }
 67         };
 68    mEditText.setOnKeyListener(list);
 69    mEditText1.setOnKeyListener(list);
 70         
 71         
 72         
 73         listner=new OnCheckedChangeListener() {
 74             
 75             @Override
 76             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 77                 // TODO Auto-generated method stub
 78                 switch (buttonView.getId()) {
 79                 case R.id.Plus:
 80                     if(!isEmpty(mEditText,mEditText1))
 81                     {
 82                         Confirm();
 83                          mBox1.setChecked(false);
 84                         return;
 85                     }
 86                     break;
 87                 case R.id.Cut:
 88                     if(!isEmpty(mEditText,mEditText1))
 89                     {
 90                         Confirm();
 91                          mBox2.setChecked(false);
 92                         return;
 93                     }
 94                     break;
 95                 case R.id.Ride:
 96                     if(!isEmpty(mEditText,mEditText1))
 97                     {
 98                         Confirm();
 99                          mBox3.setChecked(false);
100                         return;
101                     }
102                     break;
103                 case R.id.Except:
104                     if(!isEmpty(mEditText,mEditText1))
105                     {
106                         Confirm();
107                          mBox4.setChecked(false);
108                         return;
109                     }
110                     break;
111                 default:
112                     break;
113                 }
114                 
115                 if(mBox1.isChecked())
116                 {
117                     mTextView.setText( GetOperation(" "));
118                 }
119                 else
120                 {
121                     mTextView.setText("");
122                 }
123                 if(mBox2.isChecked())
124                 {
125                     mTextView2.setText(GetOperation("-"));
126                 }
127                 else
128                 {
129                     mTextView2.setText("");
130                 }
131                 if(mBox3.isChecked())
132                 {
133                     mTextView3.setText(GetOperation("*"));
134                 }
135                 else
136                 {
137                     mTextView3.setText("");
138                 }
139                 if(mBox4.isChecked())
140                 {
141                     mTextView4.setText(GetOperation("/"));
142                 }
143                 else
144                 {
145                     mTextView4.setText("");
146                 }
147             }
148         };
149         mBox1.setOnCheckedChangeListener(listner);
150         mBox2.setOnCheckedChangeListener(listner);
151         mBox3.setOnCheckedChangeListener(listner);
152         mBox4.setOnCheckedChangeListener(listner);
153         
154     }
155     public String GetOperation(String Operation)
156     {
157          if(Operation==" ")
158          {
159              Temp=Float.parseFloat(mEditText.getText().toString())
160              Float.parseFloat(mEditText1.getText().toString());
161               
162          }
163          if(Operation=="-")
164          {
165              Temp=Float.parseFloat(mEditText.getText().toString())-
166              Float.parseFloat(mEditText1.getText().toString());
167             
168          }
169          if(Operation=="*")
170          {
171              Temp=Float.parseFloat(mEditText.getText().toString())*
172              Float.parseFloat(mEditText1.getText().toString());
173          }
174          if(Operation=="/")
175          {
176              Temp=Float.parseFloat(mEditText.getText().toString())/
177              Float.parseFloat(mEditText1.getText().toString());
178          }
179          Experssion=mEditText.getText().toString() Operation mEditText1.getText().toString()
180           "=" Temp.toString();
181         return Experssion;
182     }
183  
184     
185     public void Confirm()
186     { 
187             new AlertDialog.Builder(CheckBoxCalc.this)
188             .setTitle("提示")
189             .setMessage("該項不能為空")
190             .setPositiveButton("確定", new DialogInterface.OnClickListener() {
191                 
192                 @Override
193                 public void onClick(DialogInterface dialog, int which) {
194                     // TODO Auto-generated method stub
195                     
196                 }
197             })
198             .create().show();  
199     }
200     public boolean isEmpty(EditText e,EditText a)
201     {
202         if(e.getText().toString().length()>0 && a.getText().toString().length()>0)
203         {
204             isbool= true;
205         }
206         else
207         {
208             isbool=false;
209         }
210         return isbool;
211     }
212 }

 

至此功能全部完成,是不是很簡單,趕快去試試吧。。。。

源碼下載:點擊/Files/TerryBlog/CheckBoxCalc.rar

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved